-->

DYNAMIC MEMORY ALLOCATION

3 minute read

    DYNAMIC MEMORY ALLOCATION


What is dynamic memory allocation. 
·         The process of allocate the memory at run time is known as dynamic memory allocation.

The memory allocation functions
a.    Malloc(): allocates request size of bytes and returns a pointer to the first byte of the allocated spae.
b.   calloc(): allocates space for an array of elements, initializes them to zero and then returns a pointer to the memory.
c.    free(): frees previously allocated space.
d.   realloc():modifies the size of previously allocated space.

Explain the malloc
·      We can allocated a block of memory using the function malloc.
·      The function malloc reserve a block of memory of specified size and returns a pointer of type void. It means, we can assign any type of pointer.
·      It takes the following form.
ptr=(cast_type*)malloc(byte_size);
·      ptr is a pointer of type cast_type.
·      Example
X=(int *) malloc (50 *sizeof(int));
·      Successful implementation of the above statement, a memory space is reserved to “50 times the size of an int” and the address of the first byte of the allocated memory is assigned to the integer type pointer x.
·      Similarly the statement
Cptr =(char*)malloc(10);
Allocate 10 bytes of space for the pointer cptr of type char.
·      We may also use malloc to allocate space for complex data type such as structures.

Example
Struct_var =(struct store *)malloc(sizeof(struct store));
·      Where, struct_var is a pointer of type struct store.
·      The malloc allocates a block of contiguous bytes.
·      The allocation of memory can fail if the space in the heap is not sufficient.
·      If it fails, it returns a NULL.

Explain the memory allocation using calloc
·      Calloc is used for requesting memory space at run time for storing derived data types such as arrays and structures.
·      It allocates several blocks of storage, the same size.
·      The general form of calloc is
·      Ptr=(cast type *) calloc (n,element_size);
·      The above statement allocates the same space for n blocks, element-sized bytes of each size. All bytes start at zero and the pointer of the
first byte of the allotted region returns.
·      If there is not enough space then it return NULL pointer.
·      The following segment of a program allocates space for a structure variable.
·      struct student
{
         Char name[20];
float marks;
long int rollno;
};
typedeft struct student record;
record *struct_ptr;
int class_size=30;
str_ptr=(record*)calloc(class_size, sizeof(record));
·      Record is of type struct student having three members name,marks and rollno.
·      The calloc allocates memory for 30 records. We must be sure that the requested memory has been , allocated successfully before using the str_ptr.

Explain the free()
·      We use dynamic run time allocation so it is our duty to free the space when it is not required.

·      If we release the storage space than it becomes important when the storage is limited.
·      When there is no need the data we stored in a block of memory than  we can release that block of memory for further use, using the free function.
free(ptr);
·      here ptr is a pointer, which is created by malloc or calloc.
·      If we use an invalid pointer in the call it may create problems and cause system crash.

Explain the use of realloc
Or
         How to alter the size of the dynamically allocated memory?
·      Case where the realloc function is needed are ,
Ø It is possible that the previously allocated memory is not sufficient and we need additional space for more elements.
Ø It is also possible that the memory allocated is much larger than necessary and we want to reduce it.
·      In both the cases, we can change the memory size already allocated with the help of the function realloac. This process is called the reallocation of memory.
·      For example, if we allocate the memory using following statement.
·      ptr=malloc(size)
·      then reallocation of space done by the following statement.
·      ptr=realloc(ptr,newsize)
·      realloc function allocates a new memory space to the pointer variable ptr. The newsize may be larger or smaller then the older size.
·      The new memory block's starting point may or may not begin at the same place as the old one.
·      If it is not able to find additional space in the same memory area than it will create the same space in an entirely new memory area and move the contents into the new block. The function guarantees that the old data will not loss.
·      If the function is unsuccessful in locating additional space, it returns a NULL pointer and the original block is lost.