Wednesday, 23 April 2014

Static and Dynamic allocation in C and C++

Operating system allocates certain amount of memory for each program and it doesn't grow at run time.
This memory is divided into 4 blocks
* heap
* stack ----contains local variables and function stack frames
* static/global variables
* code ---- contains instructions

when a program is executed, certain amout of memory from stack is taken and allocated for main() method. This is called stack frame.
Similarly stack frame is created for each function called above the main()'s stack frame.

when a function is called, it is pushed onto the stack.
when control returns from function, it is popped from stack.
Whatever is on the top of the stack is executing and this is called function call stack.

Also, we need to know the size of local variable at compile time only.

For ex:
int n;
cin>>n
int a[n];   //wrong as we cannot allocate memory to the array at run time.

At the time of program compilation itself, the amout of memory to be allocated for each function's stack frame is decided by the compiler and do not change!!!!

If there is a bug in the program, a recursive function calls itself  indefinitely then the entire stack memory may be filled with function stack frames and  stack may overflow and program crashes!!!!!!!

Hence it is called Static allocation.

Dynamic Memory allocation

if you want memory to be kept as long as you want, instead of getting cleared automatically when popped from stack.
Also if you want allocate the size of an array/object at run time i.e, dynamically

Done using new and delete operators in C++

for ex:
int a;
int *p;
p=new int;   //new allocates memory of size int on the heap.
*p=100;
delete p;   //frees the allocated space.

we need to explicitly remove the memory on heap as it donot get erased!!!!!

Ex:2

p= new int[20];
delete[] p;

In C, dynamic memory allocation is done using
1. malloc
2.calloc
3. realloc

void * malloc(size_t size)
malloc returns a void pointer that has address of first byte of memory of the block of memory allocated on heap , so we typecast as shown below
ex:

int *p;
p= (int *) malloc(20*sizeof(int));

we can also write
p= (int *)malloc(80); but the size of int variable varies from computer to computer..so to make our program robust we donot use this way.

free(p); // to deallocate the memory.

calloc is same as malloc except

1. it takes 2 arguments. For ex: p=(int *)calloc(20, sizeof(int));
2. it initializes all the memory allocated on heap to 'zero'

realloc is used to change the amout of memory previously allocated on heap
syntax:
void * realloc(void * ptr, size_t size)
ptr is pointer to existing block

size is the size of new block

size_t is data type that has only positive values For ex: unsigned integer.

malloc, realloc, calloc can be used in C++ also as C++ is superset of C

Entire content above is the summary of the lecture given in below Link  https://www.youtube.com/watch?v=xDVC3wKjS64

No comments:

Post a Comment