Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is stored on heap and what is stored on stack? [closed]

Tags:

Can anyone clearly explain, in terms of C,C++ and Java. What all goes on stack and what all goes on Heap and when is the allocation done.

As far as I know,

All local variables whether primitives,pointers or reference variables per function call are on a new stack frame.

and anything created with new or malloc goes on heap.

I am confused about few things.

Are references/primitives which are members of an object created on heap also stored on heap ?

and what about those local members of a method that are being recursively created in each frame. Are they all on stack, If yes then is that stack memory allocated at runtime ? also for literals, are they part of the code segment ? and what about globals in C, static in C++/Java and static in C .

like image 865
Amogh Talpallikar Avatar asked Jan 02 '12 11:01

Amogh Talpallikar


People also ask

Which objects stored in heap or stack?

Whenever an object is created, it's always stored in the Heap space and stack memory contains the reference to it. Stack memory only contains local primitive variables and reference variables to objects in heap space.

What data is stored on the heap?

Heap memory is a Dynamic memory(its size changes as program run) used to store arrays, global variables(with global scope/accessible from any function) and any created class instances(objects) at runtime in Java which are referred by the reference variables from Stack memory.

What things are stored in the stack?

In stack, variables are declared, stored and initialized during runtime. It is a temporary storage memory. When the computing task is complete, the memory of the variable will be automatically erased. The stack section mostly contains methods, local variable, and reference variables.

What is stored in heap and what is stored in stack?

The Heap Space contains all objects are created, but Stack contains any reference to those objects. Objects stored in the Heap can be accessed throughout the application. Primitive local variables are only accessed the Stack Memory blocks that contain their methods.


1 Answers

Structure of a Program in Memory

The following is the basic structure of any program when loaded in the memory.

 +--------------------------+  |                          |  |      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        |  |                          |  +--------------------------+ 

Few points to note:

  • 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

Data Segment

The data segment contains the global and static data that are explicitly initialized 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 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.

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 variables in the data area does not have such runtime overload because the initialization is done only once and that too at loading time.

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.

The attempt to write to code area leads to undefined behavior. For example (I'm going to give only C based examples) the following code may result in runtime error or even crash the system.

int main() {     static int i;     strcpy((char *)main,"something");     printf("%s",main);     if(i++==0)     main(); } 

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  } 

Or consider a still more complex example,

// 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); } 

Hope this helps!

like image 185
Sangeeth Saravanaraj Avatar answered Oct 05 '22 06:10

Sangeeth Saravanaraj