Dynamic Memory Allocation | Memory Management in C

A statically allocated variable or array has a fixed size in memory.

Dynamic Memory Allocation is a way in which the size of a data structure can be changed during the runtime.

Memory assigned to a program in a typical architecture can be broken down…


This content originally appeared on DEV Community and was authored by Anuj Raghuwanshi

A statically allocated variable or array has a fixed size in memory.

Dynamic Memory Allocation is a way in which the size of a data structure can be changed during the runtime.

Memory assigned to a program in a typical architecture can be broken down into four segments:

- Code
- Static/global variables
- Stack
- Heap

Dynamic memory allocation takes place in Heap.
Static memory allocation takes place in Stack.

Functions for dynamic memory allocation:

1. Malloc
2. Calloc
3. Realloc
4. Free

malloc() stands for memory allocation.

It reserves a block of memory with the given amount of bytes,sizeof operator is used.
Syntax:
ptr = (ptr-type*) malloc(size-in-bytes);

Image description

The return value is a void pointer to the allocated space.
Therefore, the void pointer needs to be casted to the appropriate type as per the requirements.
However, if the space is insufficient, allocation of memory fails and it returns a NULL pointer.
All the values at allocated memory are initialized to garbage values.

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

int main() {
    int *ptr;
    int n;

    printf("Enter size: \n");
    scanf("%d", &n);

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

    for (int i = 0; i < n; i++) {
        printf("Enter the value no %d of array\n", i);
        scanf("%d", &ptr[i]);
    }

    for (int i = 0; i < n; i++) {
        printf("The value at %d is %d\n", i, ptr[i]);
    }

    return 0;
}

calloc() stands for contiguous allocation.

It reserves n blocks of memory with the given amount of bytes.
It initializes each block with a default value ‘0’.
It has two parameters or arguments as compare to malloc().

Image description

The return value is a void pointer to the allocated space.
Therefore, the void pointer needs to be casted to the appropriate type as per the requirements.
However, if the space is insufficient, allocation of memory fails and it returns a NULL pointer.
Syntax:
ptr = (ptr-type*) calloc(n, size-in-bytes);

realloc() stands for reallocation.

If the dynamically allocated memory is insufficient, we can change the size of previously allocated memory using realloc() function.
Syntax:
ptr = (ptr-type*) realloc(ptr, new-size-in-bytes);

Image description

free()

free is used to free the allocated memory.
If the dynamically allocated memory is not required anymore, we can free it using free function.
This will free the memory being used by the program in the heap.
Syntax:
free(ptr)

Image description


This content originally appeared on DEV Community and was authored by Anuj Raghuwanshi


Print Share Comment Cite Upload Translate Updates
APA

Anuj Raghuwanshi | Sciencx (2024-09-08T06:59:39+00:00) Dynamic Memory Allocation | Memory Management in C. Retrieved from https://www.scien.cx/2024/09/08/dynamic-memory-allocation-memory-management-in-c/

MLA
" » Dynamic Memory Allocation | Memory Management in C." Anuj Raghuwanshi | Sciencx - Sunday September 8, 2024, https://www.scien.cx/2024/09/08/dynamic-memory-allocation-memory-management-in-c/
HARVARD
Anuj Raghuwanshi | Sciencx Sunday September 8, 2024 » Dynamic Memory Allocation | Memory Management in C., viewed ,<https://www.scien.cx/2024/09/08/dynamic-memory-allocation-memory-management-in-c/>
VANCOUVER
Anuj Raghuwanshi | Sciencx - » Dynamic Memory Allocation | Memory Management in C. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/09/08/dynamic-memory-allocation-memory-management-in-c/
CHICAGO
" » Dynamic Memory Allocation | Memory Management in C." Anuj Raghuwanshi | Sciencx - Accessed . https://www.scien.cx/2024/09/08/dynamic-memory-allocation-memory-management-in-c/
IEEE
" » Dynamic Memory Allocation | Memory Management in C." Anuj Raghuwanshi | Sciencx [Online]. Available: https://www.scien.cx/2024/09/08/dynamic-memory-allocation-memory-management-in-c/. [Accessed: ]
rf:citation
» Dynamic Memory Allocation | Memory Management in C | Anuj Raghuwanshi | Sciencx | https://www.scien.cx/2024/09/08/dynamic-memory-allocation-memory-management-in-c/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.