0
0
Embedded Cprogramming~5 mins

Stack vs heap in embedded context

Choose your learning style9 modes available
Introduction

Stack and heap are two ways your program stores data in memory. Knowing the difference helps you write better embedded programs that use memory safely and efficiently.

When you need temporary data that disappears after a function finishes, use the stack.
When you need memory that lasts longer or changes size during the program, use the heap.
When you want to avoid running out of memory in small embedded devices.
When you want to understand why your embedded program crashes or behaves oddly.
When you want to manage memory carefully to save power and space.
Syntax
Embedded C
/* Stack example: local variables inside a function */
void exampleFunction() {
    int localVariable = 10;  // Stored on stack
}

/* Heap example: dynamic memory allocation */
#include <stdlib.h>

void exampleHeap() {
    int *pointer = (int *)malloc(sizeof(int));  // Allocated on heap
    if (pointer != NULL) {
        *pointer = 20;
        free(pointer);  // Free heap memory
    }
}

The stack stores local variables and function calls. It grows and shrinks automatically.

The heap is used for dynamic memory you allocate and free manually using malloc and free.

Examples
This shows a simple local variable stored on the stack.
Embedded C
void stackExample() {
    int number = 5;  // Stored on stack
}
This shows how to allocate and free memory on the heap.
Embedded C
#include <stdlib.h>

void heapExample() {
    int *pointer = (int *)malloc(sizeof(int));
    if (pointer != NULL) {
        *pointer = 10;
        free(pointer);
    }
}
Stack can be very small if no local variables are used.
Embedded C
void emptyStackFunction() {
    // No local variables, stack usage is minimal
}
Allocating zero bytes on heap may return NULL or a pointer. Always check before use.
Embedded C
#include <stdlib.h>

void heapNullCheck() {
    int *pointer = (int *)malloc(0);  // May return NULL or valid pointer
    if (pointer != NULL) {
        free(pointer);
    }
}
Sample Program

This program shows how a variable is stored on the stack inside a function and how memory is allocated and freed on the heap. It prints values before and after to show the flow.

Embedded C
#include <stdio.h>
#include <stdlib.h>

void printStackExample() {
    int localVariable = 42;  // Stored on stack
    printf("Stack variable value: %d\n", localVariable);
}

void printHeapExample() {
    int *heapVariable = (int *)malloc(sizeof(int));  // Allocate on heap
    if (heapVariable == NULL) {
        printf("Heap allocation failed\n");
        return;
    }
    *heapVariable = 99;
    printf("Heap variable value: %d\n", *heapVariable);
    free(heapVariable);  // Free heap memory
}

int main() {
    printf("Before stack example\n");
    printStackExample();
    printf("After stack example\n\n");

    printf("Before heap example\n");
    printHeapExample();
    printf("After heap example\n");

    return 0;
}
OutputSuccess
Important Notes

Time complexity: Accessing stack variables is very fast (constant time). Heap allocation and freeing can be slower and may fragment memory.

Space complexity: Stack size is limited and fixed per program. Heap size depends on available memory and can grow or shrink.

Common mistake: Forgetting to free heap memory causes memory leaks, which is bad for embedded devices with limited memory.

Use stack for small, short-lived data. Use heap for large or variable-sized data that needs to live longer.

Summary

The stack stores temporary data like local variables and function calls.

The heap stores dynamic data you allocate and free manually.

In embedded systems, managing stack and heap carefully prevents crashes and saves memory.