Dynamic Memory Allocation in C:

Dynamic Memory Allocation in C is a process in which we allocate or deallocate a block of memory during the run-time of a program. 
The concept of dynamic memory allocation in c language enables the C programmer to allocate memory at runtime.

Dynamic Memory Allocation in CManaging Memory Efficiently

Dynamic memory allocation is a fundamental concept in the C programming language that allows you to allocate and deallocate memory dynamically during program execution. It provides flexibility and efficient memory management, enabling the creation of data structures of varying sizes and optimizing resource usage. In this article, we will explore dynamic memory allocation in C, including the functions involved, memory allocation/deallocation syntax, and examples to illustrate its usage.


Memory Allocation Functions:

C provides several functions for dynamic memory allocation, available in the standard library `<stdlib.h>`. The most commonly used functions are `malloc()`, `calloc()`, and `realloc()`.


1. `malloc()`:

 The `malloc()` function is used to allocate a block of memory of a specified size in bytes. It returns a pointer to the beginning of the allocated memory block or `NULL` if the allocation fails.


Syntax for `malloc()`:

```c

void *malloc(size_t size);

```


2. `calloc()`: 

The `calloc()` function is used to allocate a block of memory for an array, initializing all its bytes to 0. It takes two arguments: the number of elements and the size of each element. It returns a pointer to the beginning of the allocated memory block or `NULL` if the allocation fails.


Syntax for `calloc()`:

```c

void *calloc(size_t num, size_t size);

```


3. `realloc()`:

 The `realloc()` function is used to change the size of a previously allocated memory block. It takes two arguments: a pointer to the previously allocated memory block and the new size in bytes. It returns a pointer to the beginning of the reallocated memory block or `NULL` if the allocation fails.


Syntax for `realloc()`:

```c

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

```


Memory Allocation/Deallocation Syntax:

To allocate memory dynamically, you assign the return value of the memory allocation functions to a pointer of the appropriate type. The pointer can then be used to access and manipulate the allocated memory.


Syntax for memory allocation:

```c

pointer_type *ptr = (pointer_type *) malloc(size);

```

Here's an example of dynamically allocating memory for an integer:


Syntax:

```c

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

```


In this example,

 we allocate memory for an integer using `malloc()` and assign the returned pointer to `ptr`. The `sizeof(int)` determines the size of the allocated memory block.

To deallocate dynamically allocated memory and free up system resources, you use the `free()` function. It takes a pointer to the memory block as an argument.


Syntax for memory deallocation:

```c

free(ptr);

```


Example of Dynamic Memory Allocation:

Here's an example that demonstrates dynamic memory allocation for an integer array and its subsequent deallocation:


program:

```c

#include <stdio.h>

#include <stdlib.h>


int main() {

    int n;

    printf("Enter the size of the array: ");

    scanf("%d", &n);

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

    if (arr == NULL) {

        printf("Memory allocation failed!\n");

        return 1;

    }

    printf("Enter %d integers:\n", n);

    for (int i = 0; i < n; i++) {

        scanf("%d", &arr[i]);

    }

    printf("Entered integers are: ");

    for (int i = 0; i < n; i++) {

        printf("%d ", arr[i]);

    }

    free(arr);


    return 0;

}

```


In this example,

 we dynamically allocate memory for an integer array based on user input. The `malloc()` function allocates `n * sizeof(int)` bytes of memory. We then read `n` integers from the user and print them. Finally, we deallocate the memory using `free()` to release the allocated memory back to the system.


Explanation

Dynamic memory allocation provides flexibility and efficient memory management in C. It allows you to create data structures of varying sizes, adapt to changing requirements, and optimize resource utilization. Understanding the functions and syntax associated with dynamic memory allocation is essential for effective memory management and robust programming in C.

Dynamic Memory Allocation in C