Structure of a C-Program in Memory | How Heap,Stack,Data and Code segments are stored in memory?

This is most confusing question for and beginner of c programming language.  They dont know theall segment name and dont know on which segment which data are going to store . So here i am just giving you the brief idea about the structure of any c program in memory.  

This structure can be divided in 3  parts .

  • Data Segment
    • Initialized data segment (initialized to explicit initializers by programmers)
    • Uninitialized data segment (initialized to zero data segment – BSS [Block Start with Symbol])
  • Code Segment
  • Stack and Heap areas


1> Data Segment

The data segment contains teh global and static data that are explicitly intialized by the users containing the intialized values.

The other part of data segment is called BSS (because of the old IBM systems had that segment intialized to zero). It is the part of memory where the OS initializes the memory block to zeros. That is how the uninitialized global data and static get default value as zero. This area is fixed and has static size.

The data area is separated into two areas based on explicit initialization because the variables that are to be initialized can be initialized one-by-one. However, the variables that are not initialized need not be explicitly initialized with 0′s one-by-one. Instead of that, the job of initializing the variable is left to the OS. This bulk initialization can greatly reduce the time required to load the executable file.

Mostly the layout of the data segment is in the control of the underlying OS, still some loaders give partial control to the users. This information may be useful in applications such as embedded systems.

This area can be addressed and accessed using pointers from the code. Auto variables have overhead in initializing the variables each time they are required and code is required to do that initialization. However, the varialbes in the data area does not have such runtime overload because tha initialization is done only once and that too at loading time.

2>Code segment

The program code is the code area where the executable code is available for execution. This area is also of fixed size. This can be accessed only be function pointers and not by other data pointers. Another important information to note here is that the system may consider this area as read only memory area and any attempt to write in this area leads to undefined behavior.

Constant strings may be placed either in code or data area and that depends on the implementation.

3>Stack and heap areas

For execution, the program uses two major parts, the stack and heap. Stack frames are created in stack for functions and heap for dynamic memory allocation. The stack and heap are uninitialized areas. Therefore, whatever happens to be there in the memory becomes the initial (garbage) value for the objects created in that space.

Lets look at a sample program to show which variables get stored where,

 int initToZero1;
static float initToZero2;
FILE * initToZero3; 
// all are stored in initialized to zero segment(BSS)
 
double intitialized1 = 20.0;
// stored in initialized data segment
 
int main()
{
    size_t (*fp)(const char *) = strlen;
    // fp is an auto variable that is allocated in stack
    // but it points to code area where code of strlen() is stored
 
    char *dynamic = (char *)malloc(100);
    // dynamic memory allocation, done in heap
 
    int stringLength;
    // this is an auto variable that is allocated in stack
 
    static int initToZero4; 
    // stored in BSS
 
    static int initialized2 = 10; 
    // stored in initialized data segment   
 
    strcpy(dynamic,”something”);    
    // function call, uses stack
 
    stringLength = fp(dynamic); 
    // again a function call 
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值