Pages

Tuesday, December 28, 2010

Dynamic memory allocation

The process of allocating memory at run time is known as "Dynamic memory allocation". C uses some memory management functions, which can be used to allocate and deallocate memory during the program execution. This a very useful method for handling memory related tasks since the memory is allocated "on demand". It is very different from static allocation where the memory is allocated at the time of compilation. This method is mainly used when number of data items can change during the execution of a program. For example,the number of goods a customer has bought can increase or decrease during the shopping at any time. When the list grows we need to provide it more space to accommodate additional data items. Such situations can be handled move easily by using dynamic techniques. Dynamic allocation optimizes the storage space. The following functions are used in c for purpose of memory management.


Dynamic Memory Management Functions
  1. malloc : Allocates memory requests size of bytes and returns a pointer to the 1st byte of allocated space.
  2. calloc : Allocates space for an array of elements initializes them to zero and returns a  pointer to the memory.
  3.  free : Frees previously allocated space
  4.  realloc : Modifies the size of previously allocated space.

Memory allocations process:
All the program instructions and global and static variables are stored in a permanent storage area whereas local variables are stored in stacks. The memory space that is located between these two regions in available for dynamic allocation during the execution of the program. As told earlier, this free memory region is called the "heap memory" or the "free store". The size of heap keeps changing when program is executed due to creation and termination of variables that are local for a particular function and blocks (for example a variable declared inside a for loop). Therefore it is possible to encounter memory overflow during dynamic allocation process. In such situations, the memory allocation functions mentioned above will return a null pointer. Allocating a block of memory:

malloc
To allocate a block of memory we can use the "malloc" function which is found in the header file "malloc.h". The malloc function creates a block of memory of specified size and returns a pointer pointing to the first byte of the allocated memory. For example, if the address of the first allocated byte is 1001, the malloc function will return 1001. We must cast(convert) this address before using it as evident from the syntax given below.

Syntax:

ptr=(cast-type*)malloc(size);
where ptr is a pointer of type casttype. The malloc() returns a pointer (of cast type) to an area of memory with size size.

Example:
ptr=(float *) malloc(sizeof(float));

The above code will allocate a memory that is sufficient to store a float value (4 bytes in most cases) and returns the address of the first byte of memory allocated is assigned to the pointer ptr.

To allocate more memory you can use the following statement

ptr=(float *) malloc(10*sizeof(float));
This statement will allocate 40 bytes of memory since we have asked it to allocate 10*4 bytes.

calloc()

Allocating multiple blocks of memory: The function "calloc()"  is  used to request multiple blocks of storage each of the same size and then sets all bytes to zero.

Its syntax is :

ptr=(casttype*) calloc(n,size);


As you can see the syntax is almost similar to "malloc()" but the difference is that when "calloc()" is used it   allocates contiguous space for n blocks each size of "size" bytes and intialises each byte with a "0" (Zero).

free()
Releasing the used space: Dynamic allocation is of no use if we can not deallocate or delete the memory which is of no longer required. Deleting such memory blocks will keep the "heap" ever ready with memory to handle any future demands. To delete such memory the function "free()" is used. This function should be passed with the address of the memory that we want to delete.
Syntax :
free(ptr);

where ptr is a pointer that was created using malloc() or calloc(). For example, to deallocate the memory which was allocated using the "malloc()" function above, we will write:

free(ptr)

This statement will release 4 bytes of memory which can be used for any future allocation.

realloc()
To modify the size of any existing allocated memory:  There can be some conditions where you find that the memory which was  allocated by  malloc or calloc is insufficient or in excess. In this case we can change the memory size using the function "realloc". This process is called reallocation of memory.
Syntax:
ptr=realloc(ptr,newsize);

This function allocates new memory space of size newsize to the pointer variable ptr and returns a pointer to the first byte of the memory block. The allocated new block may be or may not be at the same region.

0 comments:

Post a Comment