DMA stands for Dynamic Memory Allocation, which is used to allocate the memory dynamically
(i.e, the memory size can be changed). Generally we declared a variable and store the data in it but that memory which we have used it static i.e, that memory can't be changed but in DMA the memory which are used can be resized.
There are four pre-defined functions:
(i.e, the memory size can be changed). Generally we declared a variable and store the data in it but that memory which we have used it static i.e, that memory can't be changed but in DMA the memory which are used can be resized.
Why DMA ?
- It prevents the overflow or underflow of memory i.e, when we allocates the size of any memory than sometimes the memory which has been allocated gets less or more. To prevent these type of problems DMA is used.
- DMA saves the memory which are getting wasted, also when we don't need the existed data we can de-allocates the data to use that memory for something else.
- Programmer doesn't need to know the required memory space. So it prevents the unnecessary updating of the programs.
There are four pre-defined functions:
- malloc( )
It stands for "memory allocation". It is a library function that allocates the memory dynamically and without allocating memory using malloc( ), we cannot use realloc( ) function.
Syntax: datatype *ptr;
ptr = (datatype *) malloc(n* sizeof(datatype));
//where "ptr" is the name of pointer and "n" is number of element. - calloc( )
It stands for "contiguous allocation". It is also a library function that allocates the memory dynamically and without using either calloc( ) or malloc( ), we cannot use realloc( ) function. calloc( ) and malloc( ) functions have the similar properties but there are some difference between them and one of them is that malloc( ) is faster than calloc( ).
Syntax: datatype *ptr;
ptr = (datatype *) calloc(n, sizeof(datatype)); - realloc( )
It stands for "re-allocation". It is use to re-size the memory which is allocated by using either malloc( ) or calloc( ) function. The new size can be provided by the parameter "n" used below.
Syntax: datatype *ptr;
ptr = (datatype *) ralloc(ptr, n*sizeof(datatype)); - free( )
This function is used to de-allocate the memory which have been allocated by either malloc( ) or calloc( ).
Syntax: free(name_of_the_variable);
Difference between malloc( ) and calloc( )
Malloc | Calloc |
---|---|
malloc( ) has one argument. | calloc( ) has two argument. |
Default value for malloc( ) is Garbage Value. | Default value for calloc( ) is 0. |
Syntax of malloc( ) is ⟶ int *ptr; ptr = (datatype *) malloc(n* sizeof(datatype)); |
Syntax of calloc( ) is ⟶ int *ptr; ptr = (datatype *) calloc(n, sizeof(datatype)); |
malloc( ) allocates n bytes of memory, if the allocation is succeed, a void pointer to the allocated memory is returned. Otherwise NULL is returned | calloc( ) allocates continuous block of memory large enough to hold "n" elements of size bytes each. The allocated region initializes with "ZERO". |
malloc( ) is faster than calloc( ). | calloc( ) is little slower than malloc( ) because it first initializes the allocate memory to "Zero" but practically the time difference between two of them is very little. |
Things To Remember
- To use the Dynamic functions, we have to include the header file
#include<stdlib.h>
or
#include<alloc.h> - We use pointers for Dynamic Memory Allocation.
- To use realloc( ) function the memory should be first initialized by using malloc( ) or calloc( ) function.
- Default value of malloc( ) is Garbage Value, whereas for calloc( ) it is Zero.
For giving some suggestion please comment us or check our "Contact us" page. Also more update related to coding will be added in this Blog later on, so please get attached with this Blog by clicking the follow button. Thank you for your support and time...
0 Comments