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 The output will be: Also, an array with a specified size is allocated in contiguous blocks of memory, each block has the size for one element:

As learned so far, when declaring a basic data type or an array, the memory is automatically managed. However, there is a process for allocating memory in C 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-

How Memory Management in C works?
Dynamic Memory Allocation in C malloc() function in C free() function in C calloc() function in C calloc() vs. malloc(): Key Differences
realloc() function in C Dynamic Arrays

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 C Dynamic Memory Allocation uses the heap. The <stdlib.h> library has functions responsible for Dynamic Memory Management. Let’s discuss the above functions with their application Syntax of malloc() Function: Here,

ptr is a pointer of cast_type. The C malloc() function returns a pointer to the allocated memory of byte_size.

Example of malloc(): 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. Output:

Notice that sizeof(*ptr) was used instead of sizeof(int) in order to make the code more robust when *ptr declaration is typecasted to a different data type later. The allocation may fail if the memory is not sufficient. In this case, it returns a NULL pointer. So, you should include code to check for a NULL pointer. Keep in mind that the allocated memory is contiguous and it can be treated as an array. We can use pointer arithmetic to access the array elements rather than using brackets [ ]. We advise to use + to refer to array elements because using incrementation ++ or += changes the address stored by the pointer.

Malloc() function can also be used with the character data type as well as complex data types such as structures. For example: The free() function is called to release/deallocate memory in C. By freeing memory in your program, you make more available for use later. Output Syntax of calloc() Function: Malloc() function is used to allocate a single block of memory space while the calloc() in C is used to allocate multiple blocks of memory space. Each block allocated by the calloc() function is of the same size.

The above statement is used to allocate n memory blocks of the same size. After the memory space is allocated, then all the bytes are initialized to zero. The pointer which is currently at the first byte of the allocated memory space is returned.

Whenever there is an error allocating memory space such as the shortage of memory, then a null pointer is returned. Example of calloc(): The program below calculates the sum of an arithmetic sequence. Result:

calloc() vs. malloc(): Key Differences

Following is the key difference between malloc() Vs calloc() in C: The calloc() function is generally more suitable and efficient than that of the malloc() function. While both the functions are used to allocate memory space, calloc() can allocate multiple blocks at a single time. You don’t have to request for a memory block every time. The calloc() function is used in complex data structures which require larger memory space. The memory block allocated by a calloc() in C is always initialized to zero while in function malloc() in C, it always contains a garbage value. Syntax of realloc() Function: realloc() can also be used to reduce the size of the previously allocated memory. The above statement allocates a new memory space with a specified size in the variable newsize. After executing the function, the pointer will be returned to the first byte of the memory block. The new size can be larger or smaller than the previous memory. We cannot be sure that if the newly allocated block will point to the same location as that of the previous memory block. This function will copy all the previous data in the new region. It makes sure that data will remain safe. Example of realloc(): Whenever the realloc() in C results in an unsuccessful operation, it returns a null pointer, and the previous data is also freed. Result of C Dynamic array program at the screen: In the following program, we have created and resized a Dynamic array in C

Summary

We can dynamically manage memory by creating memory blocks as needed in the heap In C Dynamic Memory Allocation, memory is allocated at a run time. Dynamic memory allocation permits to manipulate strings and arrays whose size is flexible and can be changed anytime in your program. It is required when you have no idea how much memory a particular structure is going to occupy. Malloc() in C is a dynamic memory allocation function which stands for memory allocation that blocks of memory with the specific size initialized to a garbage value Calloc() in C is a contiguous memory allocation function that allocates multiple memory blocks at a time initialized to 0 Realloc() in C is used to reallocate memory according to the specified size. Free() function is used to clear the dynamically allocated memory.