Mastering Dynamic Memory Allocation in C/C++: A Comprehensive Guide

Dynamic memory allocation is the process of allocating memory at runtime, While in static memory allocation which is done at compile time. Dynamic memory allocation is useful for allocating memory for data structures of unknown size, such as linked lists, trees, and graphs. It is also useful for allocating memory for temporary data structures, such as buffers.

When you dynamically allocate memory, the operating system allocates a block of memory from the heap. The operating system then returns a void pointer to the allocated memory which can be type casted later. You can then use this pointer to access and manipulate the dynamically allocated memory.

In this article, we'll explore four key library functions provided by the <stdlib.h> header file: `malloc()`, `calloc()`, `realloc()`, and `free()`. These functions are essential tools for efficiently managing memory in your programs.

lets dive deep and understand about each of them in detail.

malloc : -

  • malloc() stands for memory allocation and is used to dynamically allocate a single large block of memory of a specified size.
  • Syntax: ptr = (cast-type*) malloc(byte-size)
  • The cast type is the data type for which you want to allocate memory.
  • The byte size is calculated based on the size of the data type and the number of elements you want to allocate.

Byte size is expressed in terms like if you want to allocate an integer array of size 5 the you need to multiply the size of integer data type with no of elements you want to allocate.

Program to explain the malloc in detail with output

Code for malloc

Ouput :

Output for malloc


Calloc :

  • calloc() stands for contiguous allocation and is used to dynamically allocate a specified number of blocks of memory of a specified data type.
  • It is similar to malloc() but with two key differences:
  • All elements are initialized to "0" by default.
  • Syntax: Instead of byte size it requires no of elements you want to allocate and the size of data type for which you want to allocate memory separated by comma the calculation of byte size will be done automatically.ptr = (data-type*) calloc(no of elements, sizeof(data-type)

Program to explain the calloc in detail with output

Code for calloc

Output :

Output for calloc

realloc :

  • realloc() is used to dynamically change the memory allocation of a previously allocated memory.
  • If your initially allocated memory is insufficient, you can resize it using realloc(). It preserves existing data and initializes the rest of the new memory with garbage values.
  • Syntax: realloc(ptr, new size)Program to explain the realloc in detail with output

Code for re-alloc
Code for re-alloc

Output :

Ouput for re-alloc

free :

  • free() is crucial method for dynamically de-allocating memory, which is not done automatically by using malloc() or calloc(). while in other programming language such as java and C# it is done automatically by garbage collection technique.
  • It reduces memory wastage by releasing allocated memory back to the system.
  • Syntax: free(ptr)


要查看或添加评论,请登录

Kousik Roy的更多文章

社区洞察

其他会员也浏览了