Dynamic memory allocation using pointers


What is dynamic memory allocation?
Dynamic memory allocation is the method of allocating a block of memory dynamically during the run time of a program.  And the dynamically allocated block can be freed if it is not needed.  Below are the APIs available in C library to perform dynamic memory allocation.
malloc()
calloc()
realloc()
Below is the API available in C standard library which is used to free the dynamically allocated block.
free()

Difference between heap and stack?

Whenever we do dynamic memory allocation at the run time of a program, the memory block is reserved from the area called heap.  Whereas, the memory for local variables and function arguments are allocated during the compile time itself.  And the memory area reserved for local variables and function arguments is called stack.

Difference between dynamic memory allocation and static memory allocation:
The process of allocating memory dynamically at run time is called dynamic memory allocation.  Standard library functions like malloc(), calloc() and realloc() are used to perform dynamic memory allocation.  Whereas, the process of allocating memory during the compile time is called static memory allocation.  Memory area used for static memory allocation is stack.  So, the memory for local variables and arguments are reserved from stack.  Whereas, the memory area used for dynamic memory allocation is heap.  And the dynamic memory allocation can be performed using pointers.  The space allocated for local variables and arguments are freed once the execution of the function gets over.  But, the dynamically allocated block(heaps) are controlled by users.  User has to explicitly free the dynamically allocated memory block.  If not, it might result to memory leaks.

Advantages and disadvantages of dynamic memory allocation:

Advantages:
Dynamic memory allocation allows user to allocate memory at run time
The memory area used for dynamic memory allocation is heap.  And this heap is accessible from any part of the program(scope is unlimited).
It allows user to free the unused dynamically allocated memory.

Disadvantages:
Memory leaks will occur, if user misses to free the dynamically allocated memory.
Fragmentation is an another disadvantage with dynamic memory allocation. Consider, we have 1MB of free memory in heap, but the memory blocks were not contiguous.  And if user request 1MB memory block, then he can't get the requested block since the available free memory were not contiguous.

Syntax for dynamic memory allocation:
Below are the few syntaxes to perform dynamic memory allocation
     ptr = (type *)malloc(size);
     ptr - pointer variable
     type - data type
     size - number of bytes to be allocated

     ptr = (type *)calloc(n, size);
     ptr - pointer variable
     type - data type
     n - number of block to be allocated
     size - number of bytes in each block

     new = realloc(ptr, size);
     ptr - pointer to an existing block
     new - pointer to reallocated block
     size - number of bytes to be allocated

Difference between malloc and calloc
malloc() function:
Below is the prototype of malloc() function.
     void * malloc(size_t);

malloc() is a general purpose function which is used to dynamically allocate memory for any type of data at run time.  This function allocates the requested byte of memory and returns the address of the first byte of the allocated memory block.  Below is an example for dynamic memory allocation using malloc().

     int *ptr = (int *)malloc(4 * sizeof(int));

Size of an integer is 4 bytes.  So, we have requested 16 bytes(size of 4 integers) of memory. And malloc() function returns the address of the first byte of the allocated block. The type casting(int *) is used to convert the address returned by malloc() to the type "pointer to integer".

calloc() function: 
Below is the prototype of calloc() function.
     void *calloc(size_t nmemb, size_t size);

calloc() i

Comments

Popular posts from this blog

Operators And Separators In C Language Operators in C Language:

Difference between asterisk and ampersand operators in c