A common question amongst coders new to C or C++ relates to the difference between stack and heap memory allocation. The answer lies in how the code is executed at the very lowest level.

When a program is executed, each thread is allocated a limited amount of stack space. The stack holds information used by the program, including the raw byte code executed on the processor.

Variables allocated on the stack, or automatic variables, are stored directly to this memory. Access to this memory is very fast, and it’s allocation is dealt with when the program is compiled. Large chunks of memory, such as very large arrays, should not be allocated on the stack to avoid overfilling the stack memory (known as stack overflow). Stack variables only exist in the block of code in which they were declared. For example:


void a()
{
    if(true) {
        int x = 0;
    }
    x = 1;
}

In this code, x is allocated on the stack. This value is not available outside of the if() block, so attempting to access the variable outside of the block, as above, result in a compilation error.

Variables allocated on the heap, or dynamic variables, have their memory allocated at run time (ie: as the program is executing). Accessing this memory is a bit slower, but the heap size is only limited by the size of virtual memory (ie: RAM and swap space). This memory remains allocated until explicitly freed by the program and, as a result, may be accessed outside of the block in which it was allocated. For example:

int *x;
void b()
{
    if(true) {
        x = malloc(sizeof(int));
    }
    *x = 1;
}

void c()
{
    free(x);
}

In this example, memory for the variable x is allocated when b() is called, and remains in memory until c() is called. Notice how we can set the value of x outside of the if() block in which it is allocated.

In summary, temporary variables should be allocated on the stack. It’s less mucking around with memory allocation, the code is easier to read, the memory is accessed faster and the program does not need to allocate the memory on the fly. For large variables or arrays whose size may vary, heap memory allocation is your friend. Just remember to free all of the memory allocated or you’ll end up with memory leaks.




  1. reza says:

    thanks alot…

  2. Nico says:

    Nice one. Thanks

  3. ravindra says:

    Hi

  4. sharath says:

    then if we use int x outside the if block as in the second program(instead of int *x )
    Is it stored in heap?

  5. Faizan says:

    Thanks,it was a good summary.

  6. Murali says:

    Simple and nice!!!!

  7. kavya says:

    thanks a lot……….

  8. Mahyar says:

    Nice article .Simple and right to the point.Thanks you

  9. adnan says:

    your information was really very helpful thanks a lot

  10. James IV says:

    Wow, finally it starting to make a lot more sense. None of my professors have really gone over memory management a lot, but after reading this and applying to what I know I think I just got a better understanding of not only Stacks and Heaps, but memory management

  11. Mohd says:

    Helpful, thanks

  12. legenw84it says:

    Nice and simple!!!
    Thanks.

  13. satish says:

    thnx

  14. razzaq says:

    thanks! i helped me very much .

  15. MrSparkle says:

    Answered all my questions. Thanks. :3

  16. […] read about heap memory and stack memory, link, the heap memory is allocated at the start of the program. In this program when the […]

  17. Pankaj says:

    Thanks, simple and clear

  18. Nasrin says:

    Its good to understand about stack and heap

  19. […] The difference between stack and heap memory allocation « timmurphy.org […]

  20. […] The difference between stack and heap memory allocation « timmurphy.org […]

  21. […] The difference between stack and heap memory allocation « timmurphy.org […]