0
0
Power-electronicsConceptBeginner · 4 min read

Stack and Heap in Embedded C: What They Are and How They Work

In Embedded C, the stack is a special memory area that stores temporary data like function calls and local variables, working like a stack of plates. The heap is a larger memory area used for dynamic memory allocation, where memory is requested and released manually during program execution.
⚙️

How It Works

Think of the stack as a neat pile of plates where you add or remove plates only from the top. When a function is called, its local variables and return address are pushed onto the stack. When the function ends, this data is popped off, freeing that space. This makes the stack very fast and organized but limited in size.

The heap is like a big box where you can take or put plates anywhere you want, but you have to remember where you put them. It is used for dynamic memory allocation, meaning you can ask for memory during the program run and release it when done. This gives flexibility but requires careful management to avoid memory leaks or fragmentation.

💻

Example

This example shows how local variables use the stack and how dynamic memory uses the heap in Embedded C.

c
#include <stdio.h>
#include <stdlib.h>

void function() {
    int local_var = 10;  // Stored on stack
    printf("Local variable on stack: %d\n", local_var);
}

int main() {
    function();

    int *heap_var = (int *)malloc(sizeof(int));  // Allocated on heap
    if (heap_var == NULL) {
        printf("Memory allocation failed\n");
        return 1;
    }
    *heap_var = 20;
    printf("Variable on heap: %d\n", *heap_var);

    free(heap_var);  // Free heap memory
    return 0;
}
Output
Local variable on stack: 10 Variable on heap: 20
🎯

When to Use

Use the stack for temporary data that only needs to exist while a function runs, like counters or small arrays. It is fast and automatically managed, so it is ideal for most local variables.

Use the heap when you need memory that lasts beyond a single function call or when the size of data is not known at compile time. For example, when reading data from sensors or storing variable-length messages, dynamic allocation on the heap is useful. However, heap use requires careful freeing of memory to avoid leaks, especially in embedded systems with limited memory.

Key Points

  • The stack stores local variables and function call info in a last-in, first-out order.
  • The heap allows dynamic memory allocation with manual control.
  • Stack is faster but limited in size; heap is flexible but slower and needs careful management.
  • Embedded systems often have small stack and heap sizes, so efficient use is critical.

Key Takeaways

The stack stores temporary data automatically and is fast but limited in size.
The heap allows dynamic memory allocation but requires manual management.
Use stack for local variables and heap for data needing flexible or longer storage.
Careful memory management on the heap is essential in embedded systems to avoid leaks.
Understanding stack and heap helps write efficient and reliable embedded C programs.