Wednesday, January 15, 2025
HomeTechDynamic Memory Allocation in C using malloc(), calloc

Dynamic Memory Allocation in C using malloc(), calloc

In C, dynamic memory allocation allows you to allocate memory during runtime, as opposed to compile-time. This is useful when the size of the data is unknown during program compilation.

What is Dynamic Memory Allocation?

Dynamic memory allocation involves manually requesting memory during program execution using functions like:

  • malloc()
  • calloc()
  • realloc()
  • free()

The allocated memory is typically managed in the heap segment.

1. malloc() – Memory Allocation

The malloc() (memory allocation) function allocates a single block of memory of the specified size (in bytes). It does not initialize the memory—it contains garbage values.

Syntax

void* malloc(size_t size);
  • size: The number of bytes to allocate.
  • Returns: A pointer to the first byte of the allocated memory or NULL if allocation fails.
See also  Ignoring directories in Git repositories on Windows

Example

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

int main() {
    int *arr;
    int n = 5;

    // Allocate memory for 5 integers
    arr = (int*) malloc(n * sizeof(int));
    if (arr == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }

    // Assign values
    for (int i = 0; i < n; i++) {
        arr[i] = i + 1;
    }

    // Print values
    printf("Array elements:\n");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    // Free allocated memory
    free(arr);

    return 0;
}

Output

Array elements:
1 2 3 4 5

2. calloc() – Contiguous Allocation

The calloc() (contiguous allocation) function allocates multiple blocks of memory, each of the same size, and initializes all bytes to zero.

Syntax

void* calloc(size_t num, size_t size);
  • num: Number of blocks.
  • size: Size of each block (in bytes).
  • Returns: A pointer to the allocated memory or NULL if allocation fails.

Example

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

int main() {
    int *arr;
    int n = 5;

    // Allocate memory for 5 integers
    arr = (int*) calloc(n, sizeof(int));
    if (arr == NULL) {
        printf("Memory allocation failed!\n");
        return 1;
    }

    // Assign values
    for (int i = 0; i < n; i++) {
        arr[i] = (i + 1) * 2;
    }

    // Print values
    printf("Array elements:\n");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }

    // Free allocated memory
    free(arr);

    return 0;
}

Output

Array elements:
2 4 6 8 10

Key Differences Between malloc() and calloc()

Feature malloc() calloc()
Initialization Does not initialize memory (contains garbage). Initializes memory to zero.
Number of Blocks Allocates a single block of memory. Allocates multiple blocks of memory.
Syntax malloc(size) calloc(num, size)

3. Freeing Allocated Memory

The memory allocated by malloc() or calloc() must be manually freed using the free() function to avoid memory leaks.

See also  Switch Statement in C++

Syntax

void free(void* ptr);

Example

int *ptr = (int*) malloc(10 * sizeof(int));
free(ptr); // Deallocates the memory

Best Practices

  1. Always check if the pointer returned by malloc() or calloc() is NULL before using it.
  2. Use free() to release memory after use.
  3. Avoid memory leaks by ensuring every malloc() or calloc() has a corresponding free().

Let me know if you’d like to see more examples or advanced memory management techniques! 😊

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x