Describe the memory layout of a typical C program. What are the roles of the stack, heap, uninitialized data (BSS), initialized data, and text segments?
Memory layout of a C program consists of several blocks with each block having a specific task to do. The roles of the blocks are as mentioned:-
The stack is used for static allocation in a program. It stores all the automatic variables. The values stored in stack are directly stored in the RAM. Access time for items in the stack space is very fast.
The heap is used for dynamic memory allocation. Allocation here is done at runtime and accessing the items in a heap space is slower than in stack space. The size of heap is limited to size of virtual memory.
The unintialized data(BSS) segment contains the uninitialized global and static variables. This block contains only uninitialized data.
The initialized global and static variables are stored in initialized data segment.
The text segment contains the machine code/instructions the CPU needs to execute.