🧠

Understanding Dynamic Memory in C

Oct 28, 2024

Dynamic Memory Allocation in C

Overview

Dynamic memory allocation in C allows changing the size of data structures, like arrays, during runtime. This flexibility is crucial for efficient memory management.

Key Functions

C provides four key functions for dynamic memory allocation:

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

These functions are included in the <stdlib.h> library.

Memory Allocation Functions

malloc()

  • Purpose: Allocates a single block of memory.
  • Initialization: Does not initialize memory; default garbage values.
  • Returns: void* pointer to the allocated space or NULL if allocation fails.
  • Syntax: ptr = (cast-type*) malloc(byte-size);
  • Example: int* ptr = (int*) malloc(100 * sizeof(int)); // Allocates 400 bytes for 100 integers

calloc()

  • Purpose: Allocates memory for an array of elements.
  • Initialization: Initializes each block with 0.
  • Returns: void* pointer to the allocated space or NULL if allocation fails.
  • Syntax: ptr = (cast-type*) calloc(n, element-size);
  • Example: float* ptr = (float*) calloc(25, sizeof(float)); // Allocates memory for 25 floats

free()

  • Purpose: Frees the memory allocated by malloc() or calloc().
  • Syntax: free(ptr);

realloc()

  • Purpose: Changes the size of the previously allocated memory block.
  • Maintains: Preserves existing content, initializes new blocks with garbage values.
  • Returns: Pointer to the reallocated memory or NULL if reallocation fails.
  • Syntax: ptr = realloc(ptr, newSize);

Examples

malloc() Example

#include <stdio.h> #include <stdlib.h> int main() { int* ptr; int n, i; printf("Enter number of elements:"); scanf("%d", &n); ptr = (int*) malloc(n * sizeof(int)); if (ptr == NULL) { printf("Memory not allocated.\n"); exit(0); } else { printf("Memory successfully allocated using malloc.\n"); for (i = 0; i < n; ++i) { ptr[i] = i + 1; } printf("The elements of the array are: "); for (i = 0; i < n; ++i) { printf("%d, ", ptr[i]); } } return 0; }

calloc() Example

#include <stdio.h> #include <stdlib.h> int main() { int* ptr; int n = 5, i; ptr = (int*) calloc(n, sizeof(int)); if (ptr == NULL) { printf("Memory not allocated.\n"); exit(0); } else { printf("Memory successfully allocated using calloc.\n"); for (i = 0; i < n; ++i) { ptr[i] = i + 1; } printf("The elements of the array are: "); for (i = 0; i < n; ++i) { printf("%d, ", ptr[i]); } } return 0; }

free() Example

#include <stdio.h> #include <stdlib.h> int main() { int* ptr = (int*) malloc(5 * sizeof(int)); if (ptr == NULL) { printf("Memory not allocated.\n"); exit(0); } free(ptr); printf("Memory successfully freed.\n"); return 0; }

realloc() Example

#include <stdio.h> #include <stdlib.h> int main() { int* ptr; int n = 5, i; ptr = (int*) calloc(n, sizeof(int)); if (ptr == NULL) { printf("Memory not allocated.\n"); exit(0); } for (i = 0; i < n; ++i) { ptr[i] = i + 1; } n = 10; ptr = (int*) realloc(ptr, n * sizeof(int)); if (ptr == NULL) { printf("Reallocation Failed\n"); exit(0); } for (i = 5; i < n; ++i) { ptr[i] = i + 1; } printf("The elements of the array are: "); for (i = 0; i < n; ++i) { printf("%d, ", ptr[i]); } free(ptr); return 0; }

This guide covers the basics and examples of using dynamic memory allocation functions in C, demonstrating their application and syntax.*