Skip to main content

Posts

Showing posts with the label Dynamic Memory Allocation in C

Dynamic Memory Allocation in C

As you know, you have to declare the size of an array before you use it. Hence, the array you declared may be insufficient or more than required to hold data. To solve this issue, you can allocate memory dynamically. Dynamic memory management refers to manual memory management. This allows you to obtain more memory when required and release it when not necessary. There are 4 library functions defined under <stdlib.h> for dynamic memory allocation. Function Use of Function malloc() Allocates requested size of bytes and returns a pointer first byte of allocated space calloc() Allocates space for an array elements, initializes to zero and then returns a pointer to memory free() deallocate the previously allocated space realloc() Change the size of previously allocated space malloc() The name malloc stands for "memory allocation". The function malloc() reserves a block of memory of specified size and return a point