Memory structure of a C Program

Rahul GoyalRahul Goyal
2 min read

image.png

When we create a C program, its memory management looks something like this. Here we can see different sections that are stack, heap, globals , constants and code.

Lets see a brief intro about each section :

Stack : This is memory where generally program stores variables of different data types and arrays etc. This memory is modifiable so we can change values of variables present here and that's why generally variables are stored here. Stack in C grows in reverse direction i.e. First top most memory is filled and then moves till last which is in contrast to the generally used plate stack analogy for Stack in C. ๐Ÿ˜ฎ๐Ÿ˜ฎ

Heap : This is also modifiable. Here we can allocate and deallocate memory while runtime and this kind of memory helps us to do dynamic programming in C making our codes more versatile, efficient, and useful.โœจ๐ŸŽ†

Global : When variables are defined outside of all functions they are called global variables and stored in this section. This is because these variables can be accessed by all and every function and can be modified by them.๐Ÿ’ฏ๐Ÿ’ฏ

Constant : This memory section cannot be modified and once data is stored in these memory sections they can never be modified or changed. If we try to modify these sections, compiler throws an error and our code cannot compile. โœจ๐ŸŽˆ

Code : This is generally last section of memory where actually our code is stored. This section also cannot be modified. Processor access this section to read code and execute instructions. ๐Ÿ˜Ž๐Ÿ˜Ž

The structure mentioned above is generally present but in some OS there are minor changes.

1
Subscribe to my newsletter

Read articles from Rahul Goyal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul Goyal
Rahul Goyal