Describe dynamic memory allocation in C++ with suitable example.


Q.) Describe dynamic memory allocation in C++ with suitable example.

Subject: object oriented programming

Dynamic Memory Allocation in C++

Dynamic memory allocation in C++ is a process of allocating memory during the execution of a program. It is often used to create data structures that are not known at compile-time, such as linked lists and trees.

To perform dynamic memory allocation, C++ provides a set of functions in the `header file. The most common functions aremalloc,realloc, andfree`.

1. malloc Function:

The malloc function is used to allocate a block of memory of specified size. It takes a single argument, which is the size of the memory block to be allocated in bytes. The function returns a pointer to the allocated memory block. If the allocation is successful, the pointer will not be NULL. Otherwise, it will return NULL to indicate failure.

Syntax:

void *malloc(size_t size);

Example:

// Allocate 100 bytes of memory
void *ptr = malloc(100);

// Check if the allocation was successful
if (ptr != NULL) {
  // Use the allocated memory
} else {
  // Handle the allocation failure
}

2. realloc Function:

The realloc function is used to change the size of an existing memory block that was previously allocated using malloc. It takes three arguments: the pointer to the existing memory block, the new size of the memory block, and the size of the existing memory block. The function returns a pointer to the reallocated memory block. If the reallocation is successful, the pointer will not be NULL. Otherwise, it will return NULL to indicate failure.

Syntax:

void *realloc(void *ptr, size_t new_size, size_t old_size);

Example:

// Allocate 100 bytes of memory
void *ptr = malloc(100);

// Check if the allocation was successful
if (ptr != NULL) {
  // Use the allocated memory

  // Reallocate the memory block to 200 bytes
  ptr = realloc(ptr, 200, 100);

  // Check if the reallocation was successful
  if (ptr != NULL) {
    // Use the reallocated memory
  } else {
    // Handle the reallocation failure
  }
} else {
  // Handle the allocation failure
}

3. free Function:

The free function is used to release the memory block that was previously allocated using malloc or realloc. It takes a single argument, which is the pointer to the memory block to be freed.

Syntax:

void free(void *ptr);

Example:

// Allocate 100 bytes of memory
void *ptr = malloc(100);

// Check if the allocation was successful
if (ptr != NULL) {
  // Use the allocated memory

  // Free the memory block
  free(ptr);
} else {
  // Handle the allocation failure
}

Dynamic Arrays:

Dynamic arrays are one of the most common applications of dynamic memory allocation in C++. They allow you to create an array of arbitrary size at runtime. To create a dynamic array, you can use the new operator.

Syntax:

type_name *ptr = new type_name[size];

Example:

// Create a dynamic array of 10 integers
int *arr = new int[10];

// Access the elements of the array
arr[0] = 1;
arr[1] = 2;

// Delete the dynamic array
delete[] arr;

Dynamic Structures:

Dynamic memory allocation can also be used to create dynamic structures, such as linked lists and trees. These structures allow you to create data structures that can grow and shrink as needed.

Example:

// Create a linked list node
struct node {
  int data;
  node *next;
};

// Create a new node
node *new_node = new node;

// Initialize the node
new_node->data = 10;
new_node->next = NULL;

// Add the node to the linked list
head = new_node;

Advantages of Dynamic Memory Allocation:

  • Allows you to create data structures that are not known at compile-time.
  • Provides flexibility and efficiency in memory management.
  • Enables the creation of dynamic data structures such as linked lists and trees.

Disadvantages of Dynamic Memory Allocation:

  • Can lead to memory leaks if the allocated memory is not properly freed.
  • Requires careful management of the allocated memory to avoid dangling pointers.
  • Can be less efficient than static memory allocation in some cases.

In summary, dynamic memory allocation in C++ is a powerful tool that allows you to create data structures that are not known at compile-time and provides flexibility and efficiency in memory management. However, it requires careful management to avoid memory leaks and dangling pointers.