- At runtime, we can able to decide storage capacity means we can decide the memory size whether we have to increment or decrement.
There are two types memory allocations :
- Static memory allocation
- Dynamic memory allocation
Functions used to allocate memory dynamically :
void *calloc(size_t nmemb, size_t size) void *malloc(size_t size) void free(void *ptr) void *realloc(void *ptr,size_t size)
- This memory allocation will take place at heap section.
- #include<stdlib.h> must be included during work with DMA.
- Whenever we allocating Dynamic Memory, we must deal with a pointer so that we can catch it. the where in static memory we can directly access it.
- Dynamic Memory is a contiguous memory.
- Minimum 4bytes is required to access dynamic memory.
- If we lost the address of dynamically allocated memory that memory becomes lick.
- After freeing a memory, if a pointer is still holding that memory address than that pointer is called the dangling pointer.
- So, We get data may or may not be.After freeing a memory make the pointer NULL.
- realloc allocates previously allocated memory.
- syntax :
p = realloc(previously allocated memory, the memory size you want to allocate);
- malloc allocates some extra memory for pointing purpose.
realloc (p,0) = free(p); //both is equal to each other
- If realloc fails to reallocates the memory than previously allocated memory will be untouched.