What is difference between heap and stack and memory

my understanding in that heap is made from memory which all the tasks use.The stack is present inside heap.The kernel is also stored in heap.

Hi @rtosing,
Welcome to FreeRTOS community!

In my understanding, stack and heap are both memory. In FreeRTOS world, stack is a continuous memory storing task’s information, including call stack, local variables. And heap is a allocated memory allocated by malloc() and freed by free() when neccessary. Refer to About Stack and Heap for more detail.

And Stack vs Heap Memory Allocation is a great article on GeeksForGeeks to know about general difference between stack & heap.

Thanks!

The Heap is a (normally) large section of memory that you can acquire buffers out of, and possibly put back when done. FreeRTOS will allocate any object created “dynamically” (not using the create-static version) out of memory in the Heap. This includes the Stack of any task.

A Task Stack provides the memory for function local variables in that task. The used part of it will grow and shrink as the task runs and calls functions within it, and they return.

When you create the task, you need to allocate enough memory for the task stack, or it will overrun that memory and start overwriting memory not allocated to the task, which will cause problems.

You will ALWAYS have task stacks, as that is an integral part of a task’s context. You might not have a heap (and some design rules say you should not have a heap) if you allocate everything statically.

The “kernel” is the core code of the operating system, and this will reside in PROGRAM memory, and also uses some amount of static data memory for its operation. The kernel can use the heap to create tasks and objects, but it doesn’t need the heap if you disable dynamic object creation.