Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>Structure of a Program in Memory</strong></p> <p>The following is the basic structure of any program when loaded in the memory.</p> <pre><code> +--------------------------+ | | | command line | | arguments | | (argc and argv[]) | | | +--------------------------+ | Stack | | (grows-downwards) | | | | | | | | F R E E | | S P A C E | | | | | | | | | | (grows upwards) Heap | +--------------------------+ | | | Initialized data | | segment | | | +--------------------------+ | | | Initialized to | | Zero (BSS) | | | +--------------------------+ | | | Program Code | | | +--------------------------+ </code></pre> <p>Few points to note:</p> <ul> <li>Data Segment <ul> <li>Initialized data segment (initialized to explicit initializers by programmers)</li> <li>Uninitialized data segment (initialized to zero data segment - BSS [Block Start with Symbol])</li> </ul></li> <li>Code Segment</li> <li>Stack and Heap areas</li> </ul> <p><strong>Data Segment</strong></p> <p>The data segment contains the global and static data that are explicitly initialized by the users containing the intialized values.</p> <p>The other part of data segment is called BSS (because of the old IBM systems had that segment initialized 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.</p> <p>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. </p> <p>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. </p> <p>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 variables in the data area does not have such runtime overload because the initialization is done only once and that too at loading time. </p> <p><strong>Code segment</strong></p> <p>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.</p> <p>Constant strings may be placed either in code or data area and that depends on the implementation. </p> <p>The attempt to write to code area leads to undefined behavior. For example (I'm going to give only <code>C</code> based examples) the following code may result in runtime error or even crash the system. </p> <pre><code>int main() { static int i; strcpy((char *)main,"something"); printf("%s",main); if(i++==0) main(); } </code></pre> <p><strong>Stack and heap areas</strong></p> <p>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. </p> <p>Lets look at a sample program to show which variables get stored where,</p> <pre><code>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 } </code></pre> <p>Or consider a still more complex example,</p> <pre><code>// command line arguments may be stored in a separate area int main(int numOfArgs, char *arguments[]) { static int i; // stored in BSS int (*fp)(int,char **) = main; // points to code segment static char *str[] = {"thisFileName","arg1", "arg2",0}; // stored in initialized data segment while(*arguments) printf("\n %s",*arguments++); if(!i++) fp(3,str); } </code></pre> <p>Hope this helps!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload