Dynamic Allocation

Dynamic allocation is a process of allocating memory during runtime. In C, this is done using the malloc(), calloc(), realloc(), and free() functions.

  • malloc() allocates a block of memory of the specified size. It returns a pointer to the beginning of the allocated memory.
  • calloc() allocates a block of memory of the specified size and initializes all the bytes to zero. It returns a pointer to the beginning of the allocated memory.
  • realloc() changes the size of an existing memory block. It returns a pointer to the beginning of the new memory block.
  • free() deallocates a memory block that was allocated using malloc()calloc(), or realloc().

Dynamic allocation is useful when you need to allocate memory for a variable whose size is not known at compile time. For example, you might use dynamic allocation to create a linked list, which is a data structure that can grow and shrink as needed.


Sure, here is an article about dynamic allocation in C programming language:

Dynamic Allocation in C Programming Language

Dynamic allocation is a process of allocating memory during runtime. In C, this is done using the malloc(), calloc(), realloc(), and free() functions.

  • malloc() allocates a block of memory of the specified size. It returns a pointer to the beginning of the allocated memory.
  • calloc() allocates a block of memory of the specified size and initializes all the bytes to zero. It returns a pointer to the beginning of the allocated memory.
  • realloc() changes the size of an existing memory block. It returns a pointer to the beginning of the new memory block.
  • free() deallocates a memory block that was allocated using malloc()calloc(), or realloc().

Dynamic allocation is useful when you need to allocate memory for a variable whose size is not known at compile time. For example, you might use dynamic allocation to create a linked list, which is a data structure that can grow and shrink as needed.

It is important to use dynamic allocation carefully, as it can lead to memory leaks if you do not deallocate the memory that you allocate. A memory leak is a situation where memory that is no longer needed is not deallocated. This can cause your program to run out of memory and crash.

To avoid memory leaks, you should always use the free() function to deallocate memory that you allocate using malloc(), calloc(), or realloc(). You should also use the assert() macro to check that the pointer that you are passing to the free() function is valid.

Here is an example of how to use dynamic allocation to create a linked list:

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

struct node {
  int data;
  struct node *next;
};

struct node *create_node(int data) {
  struct node *new_node = malloc(sizeof(struct node));
  new_node->data = data;
  new_node->next = NULL;
  return new_node;
}

void free_list(struct node *head) {
  struct node *current = head;
  while (current != NULL) {
    struct node *next = current->next;
    free(current);
    current = next;
  }
}

int main() {
  struct node *head = create_node(10);
  head->next = create_node(20);
  head->next->next = create_node(30);

  free_list(head);

  return 0;
}

This code creates a linked list with three nodes. The first node has the data 10, the second node has the data 20, and the third node has the data 30. The free_list() function deallocates the memory that was allocated for the linked list.

Leave a Reply

Your email address will not be published. Required fields are marked *