Memory Layout of Programs



    Whenever we execute any program the OS loads it's executable image into the RAM of the computer. In practice the architecture can be a little complex but for simplicity we can divide the memory into following segments:

  • Stack Segment

  • Heap Segment

  • Data Segment

  • Text Segment

Stack Segment

Stack uses LIFO (Last- In-First-Out) mechanism for storing local or automatic variables, function parameters and storing next address or return address. The process Stack contains the temporary data such as method/function parameters, return address and local variables. This segment grows from a higher address to a lower address.

Heap Segment

This is dynamically allocated memory to a process during its run time. This is area of memory allotted for dynamic memory storage such as for malloc() and calloc() calls in C/C++ programming. This segment size is also variable as per user allocation. This segment grows from a lower address to a higher address.

Data Segment

This section contains the global and static variables. It is represented by .data section and the .bss. The .data section is used to declare the memory region, where data elements are stored for the program. This section cannot be expanded after the data elements are declared, and it remains static throughout the program. The .bss section is also a static memory section that contains buffers for data to be declared later in the program. This buffer memory is zero-filled.

Text Segment

This includes the current activity represented by the value of Program Counter and the contents of the processor's registers. It is represented by .text section. This defines an area in memory that stores the instruction codes. This is also a fixed area.

Suhaib Bin Younis | @suhaibbinyounis

Post a Comment

Post a Comment (0)

Previous Post Next Post