Skip to main content

Dynamic Memory Allocation in C

Introduction to Dynamic Memory Allocation in C

Dynamic memory allocation in C programming allows programmers to allocate memory during runtime. In simple terms, you can dynamically create, modify, and delete blocks of memory in your code while your program is executing. This tutorial will give an overview of dynamic memory allocation and how to use it in C.

Static vs Dynamic Memory Allocation

Before we dive into dynamic memory allocation, let's briefly discuss static memory allocation for better understanding.

Static Memory Allocation: This memory allocation is done before program execution. It involves fixed-size variables. An example is array declaration where you specify the number of elements the array should hold. The downside of static memory allocation is that it can lead to unused memory if you overestimate the size you need.

Dynamic Memory Allocation: Unlike static memory allocation, dynamic memory allocation occurs during runtime. This means you can decide the amount of memory you need to allocate while the program is running. This is advantageous in cases where memory needs can not be determined ahead of time.

Functions Used in Dynamic Memory Allocation

There are four main functions used in dynamic memory allocation in C:

  • malloc()
  • calloc()
  • realloc()
  • free()

Let's discuss each of these functions in detail.

malloc()

This function stands for 'memory allocation'. It is used to dynamically allocate a single large block of memory with the specified size. It initializes each block with a default garbage value.

ptr = (cast-type*) malloc(byte-size)

calloc()

This function stands for 'contiguous allocation'. It is used for allocating multiple blocks of memory. Each block is of the same size, and all are initialized to zero.

ptr = (cast-type*) calloc(n, element-size);

realloc()

The 'realloc', or 'reallocation' function, is used to dynamically change the memory allocation of a previously allocated memory. In other words, if you've already allocated some memory using malloc() or calloc() and need to alter the size, you can use realloc().

ptr = realloc(ptr, newSize);

free()

The 'free' function helps in releasing the dynamically allocated memory. Remember, failing to free up memory after you've finished with it can lead to what is known as a 'memory leak'.

free(ptr);

Using Dynamic Memory Allocation Functions

Let's take a look at an example where we use these functions.

#include <stdio.h>
#include <stdlib.h>

int main() {
int n, i, *ptr, sum = 0;

printf("Enter number of elements: ");
scanf("%d", &n);

ptr = (int*) malloc(n * sizeof(int));

if(ptr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}

printf("Enter elements: ");
for(i = 0; i < n; ++i)
{
scanf("%d", ptr + i);
sum += *(ptr + i);
}

printf("Sum = %d", sum);

// using realloc()
ptr = realloc(ptr, n * sizeof(int));
if(ptr == NULL)
{
printf("Error! memory not re-allocated.");
exit(0);
}

// using free()
free(ptr);

return 0;
}

In the above example, we've used malloc() to allocate memory, realloc() to reallocate memory and free() to release the memory.

Conclusion

Dynamic memory allocation in C is a powerful feature that allows programmers to efficiently manage memory, particularly in complex data structures and algorithms. With the help of dynamic memory allocation, you can optimize your programs to use just the right amount of memory they need during execution.