Saturday, August 30, 2014

memory layout of c program in linux

enough information provided in below links.

link :

http://www.geeksforgeeks.org/memory-layout-of-c-program/

http://knavite.blogspot.in/2014/07/linux-memory-layout-test-through-c.html?showComment=1409400390944#c1248974729538731931


http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-1.html



more info :

Where in memory are my variables stored in c?


  • global variables -------> data 
  • static variables -------> data 
  • constant data types -----> code and/or data. Consider string literals for a situation when a constant itself would be stored in the data segment, and references to it would be embedded in the code
  • local variables(declared and defined in functions) --------> stack 
  • variables declared and defined in main function ----->  stack
  • pointers(ex: char *arrint *arr) -------> data or stack, depending on the context. C lets you declare a global or a static pointer, in which case the pointer itself would end up in the data segment , otherwise it will be in stack.
  • dynamically allocated space(using malloccallocrealloc) -------->  heap

How is the array stored in memory?



An array stores its elements in contiguous memory locations.
If You created the array locally it will be on stack. Where the elements are stored depends on thestorage specification.
For Eg:
An array declared globally or statically would have different storage specification from an array declared locally. Technically, the where part is implementation defined but usually implementations would use similar usage patterns.
  • An local array will be (usually)created on stack while
  • global or static array will be (usually)created on bss/data segments and
  • dynamically created array will be created on heap.


1 comment: