Basics of Dynamic Memory Allocation

Basics of Dynamic Memory Allocation

In this tutorial, you are going to learn about Basics of Dynamic Memory Allocation, What is Static Memory Allocation, Example of Static Memory Allocation, Problems faced in Static Memory Allocation, What is Dynamic Memory Allocation, Use of Pointers in Dynamic Memory Allocation and Built in functions for Dynamic Memory Allocation.

Static Memory Allocation

Memory allocated during compile time is called dynamic memory allocation. The memory allocated is fixed and can not be increased or decreased during the run time.

Example:

int main()
{
int arr[5] = {1,2,3,4,5};
}

Problems Faced in Static Memory Allocation

There are certain problems which could be faced in static memory allocation.

  1. If you are allocating memory for an array during compile time then you have to fix the size at the time of declaration. Size is not fixed and users can’t increase or decrease the size of the array at the run time.
  2. If the values stored by the user in the array at run time is less than the size specified then there will be wastage of memory.
  3. If the value stored by the user in the array at run time is more than the size specified then the program may crash or misbehave.

Dynamic Memory Allocation

To get rid of the problem faced during Static memory allocation Dynamic memory allocation is introduced. The process of allocating memory at the time of execution is called dynamic memory allocation. We can allocate memory according to our needs. Pointer plays a very important role in dynamic memory allocation. Allocated memory can only be accessed through Pointers.

Here comes the role of Heap which is the memory segment of memory layout of C program.

Basics of Dynamic Memory Allocation

What is Heap

Heap is the segment of the memory where dynamic memory allocation takes place. Unlike stacks where memory is allocated or unallocated in defined order, Heap is an area of the memory where memory is allocated or deallocated without any order or randomly. There are certain built-in functions available that can help in allocating or deallocating some memory space at run time.

Built in Functions for Dynamic Memory Allocation

  1. Malloc()
  2. Calloc()
  3. Realloc()
  4. free()

There are several built-in functions provided by programming languages for dynamic memory allocation. The two most commonly used functions are malloc and calloc.

The malloc function is used to allocate a block of memory of a specified size, in bytes. The syntax for malloc is as follows:

void *malloc(size_t size);

Where size is the number of bytes of memory to be allocated. The function returns a pointer to the first byte of the allocated memory, or a null pointer if the allocation fails.

The calloc function is similar to malloc, but it also initializes the allocated memory to zero. The syntax for calloc is as follows:

void *calloc(size_t nmemb, size_t size);

Where nmemb is the number of elements to be allocated and size is the size of each element, in bytes. The function returns a pointer to the first byte of the allocated memory, or a null pointer if the allocation fails.

Both malloc and calloc functions return a pointer to void which must be casted to a pointer of the appropriate type.

In addition to malloc and calloc, there are two other built-in functions for dynamic memory allocation: realloc and free.

The realloc function is used to change the size of a previously allocated block of memory. The syntax for realloc is as follows:

void *realloc(void *ptr, size_t size);

Where ptr is a pointer to the previously allocated memory and size is the new size, in bytes. The function returns a pointer to the first byte of the reallocated memory, or a null pointer if the reallocation fails.

The free function is used to deallocate a block of memory that was previously allocated using malloc, calloc, or realloc. The syntax for free is as follows:

void free(void *ptr);

Where ptr is a pointer to the memory that is to be deallocated.

It is important to note that when using dynamic memory allocation, it's a programmer's responsibility to keep track of allocated memory, and deallocate it properly when it is no longer needed. This will help to prevent memory leaks, which can cause a program to consume more and more memory over time, eventually leading to the program crashing or hanging. A common technique to prevent memory leaks is to use smart pointers, which are a type of object that automatically manage the lifetime of dynamically allocated objects and automatically release the memory when it is no longer needed.

In conclusion, dynamic memory allocation is a technique that allows programs to allocate and deallocate memory at runtime, which can be useful for many types of applications. The malloc, calloc, realloc, and free functions are built-in functions provided by most programming languages for dynamic memory allocation, which allows programmers to easily allocate, resize, and deallocate memory as needed. It is important to keep in mind that when using dynamic memory allocation, it's the programmer's responsibility to keep track of allocated memory and properly deallocate it when it is no longer needed, to prevent memory leaks.


This article on Basics of Dynamic Memory Allocation is contributed by Aditya Raj and edited by Rajnish Kumar. If you like TheCode11, then do follow us on Facebook, Twitter and Instagram.

Post a Comment

Previous Post Next Post

Contact Form