C Memory Management

There are two places where variables can be put in memory. When you create a variable like this:

int  a;
char c;
char d[16];

The variables are created in the “stack“. Stack variables are automatically freed when they go out of scope (that is, when the code can’t reach them anymore). You might hear them called “automatic” variables, but that has fallen out of fashion.

Many beginner examples will use only stack variables.

The stack is nice because it’s automatic, but it also has two drawbacks: (1) The compiler needs to know in advance how big the variables are, and (2) the stack space is somewhat limited. For example: in Windows, under default settings for the Microsoft linker, the stack is set to 1 MB, and not all of it is available for your variables.

If you don’t know at compile time how big your array is, or if you need a big array or struct, you need “plan B”.

Plan B is called the “heap“. You can usually create variables as big as the Operating System will let you, but you have to do it yourself. Earlier postings showed you one way you can do it, although there are other ways:

int size;
// ...
// Set size to some value, based on information available at run-time. Then:
// ...
char *p = (char *)malloc(size);

(Note that variables in the heap are not manipulated directly, but via pointers)

Once you create a heap variable, the problem is that the compiler can’t tell when you’re done with it, so you lose the automatic releasing. That’s where the “manual releasing” you were referring to comes in. Your code is now responsible to decide when the variable is not needed anymore, and release it so the memory can be taken for other purposes. For the case above, with:

free(p);

What makes this second option “nasty business” is that it’s not always easy to know when the variable is not needed anymore. Forgetting to release a variable when you don’t need it will cause your program to consume more memory that it needs to. This situation is called a “leak”. The “leaked” memory cannot be used for anything until your program ends and the OS recovers all of its resources. Even nastier problems are possible if you release a heap variable by mistake before you are actually done with it.

In C and C++, you are responsible to clean up your heap variables like shown above. However, there are languages and environments such as Java and .NET languages like C# that use a different approach, where the heap gets cleaned up on its own. This second method, called “garbage collection”, is much easier on the developer but you pay a penalty in overhead and performance. It’s a balance.

(I have glossed over many details to give a simpler, but hopefully more leveled answer)

Leave a Comment