DYNAMIC MEMORY ALLOCATION:
Dynamic memory allocation is the means by which a program can obtain memory while it is running. Global variables are allocated storage at compile time. Nonstatic , local variables use the stack. However, niether global nor local variables can be added during program execution. Yet there will be times when the storage needs of a program cannot not known ahead of time. For example , a program might want to use a dynamic data structure,such as a linked list or a binary tree.Such structures are inherently dynamic in nature,growing and shrinking as needed. To implement such a data structure requires that a program to be able to allocate and free memory as needed.
Memory allocated by c’s dynamic allocation functions is obtained from the heap. The heap is free memory that is not used by your program,the os,or any other proram running in your computer. The size of the heap cannot usually be known in advance,but it typically contains a very large amount of free memory.
The following functions are used in C for dynamic memory allocation:
- Alloc ( ): The alloc function is used to aloocate a region or block of size bytes in length of the heap. The general declaration of the alloc() function is:
Char *alloc(size)
Unsigned int size;
Some C compiler versions fill the allocated region with zeros. All versions return some kind of an error when there is no more space left to allocate, either 0,-1 or printing a message to the system .
- Malloc ( ): The malloc () function is used to allocate heap storage. Its name stands for memory allocation .The allocated regiojn is not filled with zeros. The starting address is returned if the function is successful. A zero is returned if the function attempts to get a block of memory fails
Char *malloc(size)
Unsigned int size;
- Calloc ( ): The calloc function is used to allocate the continuous memory or element by element basis. The name stands for calculated allocation.It is useful in arrays and array like structures where continuity of memory is required. The starting address of the area is returned if the function is successful.A zero is returned if the function attempt to get a block a memory fails.
The general declaration of calloc function is given as:
Char *calloc(elements,element size)
Unsigned int elements;
Unsigned int element_size;
- Realloc (): The realloc function is used to increase or decrease the size of the block of heap memory to the size , specific by size preserving the address and contents of the beginning of the block. The function reealloc() can diminish the size of the allocated area.
The general declaration of realloc() function is:
Char *realloc(old block,size)
Char *old block;
Unsigned int size;
The realloc function returns the address of the newly allocated region which is same as the old block. It returns a zero if an attempt to create a new block is unsuccessful.
- Free(): the free function is used to free a portion of storage within the heap previously allocated by alloc(),malloc(),calloc(),or realloc(). The storage returns to heap,making it available for further heap activity. The pointer that is the arguments to free() is the starting address for the region allocated.
Its general declaration is:
Int free (pointer)
Char *pointer;