How a variable value is stored and retrieved at runtime in C?for example main(){ int a; a=5; }Where is the value of 'a' stored and How it is retrieved at runtime?

Questions by arunkumar2003

Showing Answers 1 - 6 of 6 Answers

all the above code is in the code section of memory...as int a;a=5;are automatic storage class type..so they will come to life when this function is called.as local variables are stored in stack section so...memory will be given in the function frame created for this function...all values gets destroyed when function returns and collapse....

  Was this answer useful?  Yes

See the attached snippet for a general picture of how a compiled program is loaded into memory for x86-based systems. Some architectures (such as Harvard architecture) dont store program text and data together in the same memory, so those layouts will be different. But this should be good enough to answer your question in a general sense.

The C programming language defines three storage durations for objects in memory. Objects with automatic storage duration (i.e., local variables within a function) have storage set aside when you enter their enclosing scope (function or block), and that storage is released when you exit that scope. Objects with static storage duration (i.e., variables declared at file scope or with the static keyword) have storage set aside when the program is first loaded into memory (even before main executes), and that storage is released when the program exits. Objects with dynamic storage duration have storage set aside when you call one of malloc, calloc, or realloc, and that storage is released when you call free.

Note that I use the term "storage" instead of "memory"; Im talking about memory in the virtual, logical sense, not in terms of physical RAM.

So how do those storage durations map to the image below? The segment "Initialized data" is used for read-only data like string literals and const-qualfied static variables. These items are stored in the compiled binary file itself, so they appear in memory as soon as the program is loaded. The segment "Uninitialized data" is used for global variables that may or may not have an initial value, and are writable. Again, this space is set aside in the compiled binary file itself, so that memory is available as soon as the program is loaded. If theres no explicit initializer for those items, they will be initialized to 0 or NULL.

The stack is used to store objects with automatic storage duration; each time a function is called, a new stack frame is created on the stack. Each stack frame stores the arguments passed to the function and any local variables, along with the return address (that is, the address of the next instruction following the function call).

There are usually two registers associated with the stack. One is the stack pointer, and it contains the address of the last item pushed onto the stack. The other is the frame pointer, and it points to the overall frame (typically, it points to the address just below the last function argument). If you look at the generated machine code, youll see that function arguments and local variables are referred to by their offset from the frame pointer.

The heap is used to store objects with dynamic storage allocation; basically, anything created with malloc, calloc, or realloc. Note that there is a difference between the allocated object and the pointer variable that refers to that object. Given a declaration like "T *p = malloc( sizeof *p );", the variable "p" lives on the stack, but the object it points to lives on the heap. When you exit the function, the space for "p" will automatically be deallocated when the stack frame is popped off the stack, but the memory it pointed to will still be allocated until someone calls free on it.

Code
  1. General program layout in memory:

  2.  

  3.              +-----------------+

  4. High address |    Arguments,   |  Contents of argv array

  5.              |   environment   |

  6.              |    variables    |

  7.              +-----------------+

  8.              |      Stack      |  Automatic (local) variables

  9.              |     .......     |

  10.              |        |        |

  11.              |        V        |

  12.              |        ^        |

  13.              |        |        |

  14.              |     .......     |  Dynamically allocated memory (malloc/calloc/realloc)

  15.              |       Heap      |

  16.              +-----------------+

  17.              |  Uninitialized  |  Non-const static (global) variables

  18.              |      data       |

  19.              +-----------------+

  20.              |   Initialized   |  String literals, const-qualified static

  21.              |      data       |  variables  

  22.              +-----------------+

  23.              |   Program text  |  Functions (including main())

  24. Low address  |  (machine code) |      

  25.              +-----------------+

  26.  

  27. Stack frame:

  28.            

  29.              +-----------------+

  30. High address | Function args   |

  31.              |        .        |

  32.              |        .        |

  33.              |        .        |

  34.              +-----------------+

  35.              |  Return address | <-- Frame pointer

  36.              +-----------------+

  37.              | Local variables |

  38.              |        .        |

  39.              |        .        |

  40. Low address  |        .        | <-- Stack pointer

  41.              +-----------------+

  42.  

  43. Sample program:

  44.  

  45.     #include <stdio.h>

  46.    

  47.     void foo( void )

  48.     {

  49.       printf( "foo

  50. " );

  51.     }

  52.  

  53.     void bar( void )

  54.     {

  55.       foo();

  56.     }

  57.  

  58.     int main( void )

  59.     {

  60.       bar();

  61.       return 0;

  62.     }

  63.  

  64. When the program first enters the foo function, the stack will look

  65. something like this:

  66.  

  67.              +-----------------+

  68. High address | Previous frame  | stack frame for main

  69.              +-----------------+

  70.              | Previous frame  | stack frame for bar

  71.              +-----------------+

  72.              |  Current frame  | stack frame for foo

  73.              +-----------------+

  74.              | Available stack | will be used by printf

  75.              |      memory     |    

  76.              |        .        |

  77.              |        .        |

  78. Low address  |        .        |

  79.              +-----------------+

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions