- C Dynamic Memory Allocation using malloc(), calloc(), realloc(), free()
- Before you learn Dynamic Memory allocation, let's understand:
- How Memory Management in C works?
When you declare a variable using a basic data type,
- the C compiler automatically allocates memory space for the variable in a pool of memory called the stack.
- For example, a float variable takes typically 4 bytes (according to the platform) when it is declared.
- We can verify this information using the sizeof operator as shown in below example
#include <stdio.h> int main() { float x; printf("The size of float is %d bytes", sizeof(x)); return 0;}
The output will be:
The size of float is 4 bytes
- Also, an array with a specified size is allocated in contiguous blocks of memory, each block has the size for one element:
#include <stdio.h> int main() { float arr[10]; printf("The size of the float array with 10 element is %d", sizeof(arr)); return 0;}
The result is:
The size of the float array with 10 element is 40
- As learned so far, when declaring a basic data type or an array, the memory is automatically managed. H
- there is a process for allocating memory which will permit you to implement a program in which the array size is undecided until you run your program (runtime). This process is called
- "Dynamic memory allocation."
In this tutorial, you will learn
Dynamic Memory Allocation
- Dynamic Memory Allocation is manual allocation and freeing of memory according to your programming needs. D
- memory is managed and served with pointers that point to the newly allocated memory space in an area which we call the heap.
- Now you can create and destroy an array of elements dynamically at runtime without any problems. To sum up, the automatic memory management uses the stack, and the dynamic memory allocation uses the heap.
- The <stdlib.h> library has functions responsible for dynamic memory management.
FunctionPurpose
- mallocAllocates the memory of requested size and returns the pointer to the first byte of allocated space.
- callocAllocates the space for elements of an array. Initializes the elements to zero and returns a pointer to the memory.
- reallocIt is used to modify the size of previously allocated memory space.
- FreeFrees or empties the previously allocated memory space.
Let's discuss the above functions with their application
- The malloc Function
The malloc() function stands for memory allocation. It is a function which is used to allocate a block of memory dynamically. It reserves memory space of specified size and returns the null pointer pointing to the memory location. The pointer returned is usually of type void. It means that we can assign malloc function to any pointer.
Syntax
ptr = (cast_type *) malloc (byte_size);
Here,
ptr is a pointer of cast_type.
The malloc function returns a pointer to the allocated memory of byte_size.
Example: ptr = (int *) malloc (50)
When this statement is successfully executed, a memory space of 50 bytes is reserved. The address of the first byte of reserved space is assigned to the pointer ptr of type int.
Consider another example:
#include <stdlib.h> int main(){ int *ptr; ptr = malloc(15 * sizeof(*ptr)); /* a block of 15 integers */ if (ptr != NULL) { *(ptr + 5) = 480; /* assign 480 to sixth integer */ printf("Value of the 6th integer is %d",*(ptr + 5)); } }
Output:
Value of the 6th integer is 480