Memory Layout of a C Program

There are four segments in C Program. Data, Code, Stack and Heap Segments. All this sections define the scope & storage class of the data and flow of control between the instructions.

Data Segments: The data segment is used to hold the value of those variables that need to be available throughout the lifetime of the program, so global & static variables (which are local but are required between function calls) are stored here. The data segment is divided into two parts: the Initialized data segment & the Un-initialized data segment. All the global/static variables which are not initialized are created in the un-initialized part of the data segment which is set to zero by loader. The uninitialized segment usually referred as BSS(Block Starting with Symbol). BSS gets its name from old IBM systems that had segment initialized to zero. The layout of this segment is in the control of the underlying OS, though some loaders might give some control to the users. Pointers can be used to access the data in this segment.

Code Segment: The code segment is used to store the executable code. This area is also known as the Text Segment & is of fixed size. Only function pointers can access this area. It’s only read only segment so any attempt to write this area by user can lead to undefined behavior.

Stack segment: Here automatic variables are stored, along with information that is saved each time a function is called. Each time a function is called, the address of where to return to and certain information about the caller’s environment, such as some of the machine registers, are saved on the stack. The newly called function then allocates room on the stack for its automatic and temporary variables. This is how recursive functions in C can work. Each time a recursive function calls itself, a new stack frame is used, so one set of variables doesn’t interfere with the variables from another instance of the function.

The stack is a collection of stack frames. When a new frame needs to be added (as a result of a newly called function), the stack grows downward. Every time a function is called, an area of memory is set aside, called a stack frame, for the new function call. This area of memory holds some crucial information, like:

  1. Storage space for all the automatic variables for the newly called function.
  2. The line number of the calling function to return to when the called function returns.
  3. The arguments, or parameters, of the called function.

Heap segment: The heap is for dynamic memory allocation i.e. whenever memory is allocated using malloc/ calloc/ realloc functions etc heap area is used. The C library also gets dynamic memory for its own personal workspace from the heap as well. As more memory is requested at run-time, the heap grows upward.

The stacks and heaps are un-initialized area, so any uninitialized variables get a default value as garbage value.

This entry was posted in Placements. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *