Aug 8, 2015

Dynamic memory allocation (Stack vs Heap)


          Application’s memory – The memory that is assigned to a program or application while running is divided into 4 segments.

Heap
(dynamic allocated)
Stack
 (functions and local variables)
Global / Static
Code ( text )
(instructions)

·       The amount of memory that is reserved for main function is called as Stack Frame.
·       Heap size is not fixed.
For example :
#include<iostream>
using namespace std;
int z;                 // global variables.
int mul(int a,int b)
{
int c;
c=a*b;
return c;
}
int init()
{
int a,b,c;
scanf("%d%d",&a,&b);
c=mul(a,b);
return c;
}
int main()
{
z= init();
printf("%d",z);
return 0;
}

While the program mul() is executing, the stack will have memory assigned as mentioned below :

mul() a, b, c
scanf()
init() a, b, c,
Main

The program starts with the main() function and is pushed into the stack. In main init() function is called next, so the main function stops executing and init() function resume and its variables are also declared in its memory declared for init(). Similarly for others.
·       The size of the stack for a method is calculated when the program is compiling. At any time, the function at the top of stack is executing, remaining are idle.

·       Once the function returns, the memory in stack is popped out and next function below it, will start executing.

·       Global variables are declared outside the stack are allocated in separate global memory section. So by this we can access global variables anywhere in the program, until it terminates.

·       Local variables declared inside each function, are allocated in their functions stack memory and also limited to those functions only.

·       When main functions returns, the program terminates and the global variables are also cleared.

·       If the stack calls goes beyond the memory, then an error will occurs i.e. stack overflow.

·       The size of Heap is not fixed. It will grow as long as it don’t run out of memory in the system itself.

·       we can keep the data in the heap till what time we need i.e. we can have control on the allocation and de-allocation.

·       Heap is also called as Dynamic memory and it’s allocation is called as Dynamic memory allocation. It’s different from heap (data structure).

In c, There are 4 functions that are used in dynamic memory allocation.

They are
       1) malloc()
       2) calloc()
3) re-alloc()
4) free()
In c++, there are extra 2 more operators including above.

New and delete

For example:
int main()
{
int a;
int *p, *q;
p = (int*)malloc(sizeof(int));
*p = 10;
q = (int*)calloc(3,sizeof(int));
*q = 10;
*(q+1) = 20;
*(q+2) = 30;
return 0;
}



·       Free(p) – It will deallocate the memory that is assigned to p.
·    p will have only the starting address of that memory block.

·       If we won’t mention free(p), in c or delete in c++ , the data will be still available until the program terminates (act like global variables ).

Recent posts :