Dynamic memory allocation

  • What is dynamic memory allocation ?
  • Dynamic memory allocation refers to the process of allocating memory at runtime (when a program is executing) rather than at compile-time.

  • Dynamic memory function :
  • Function Description
    malloc Allocates a specified number of bytes of uninitialized memory.
    Syntax : int *ptr = (int *)malloc(n * sizeof(int));
    calloc Allocates a specified number of elements each of a specified size, initializing them to zero.
    Syntax : int *ptr = (int *)calloc(n, sizeof(int));
    realloc Resizes previously allocated memory.
    Syntax : ptr = (int *)realloc(ptr, new_size * sizeof(int));
    free Deallocates memory that was previously allocated dynamically.
    Syntax : free(ptr);

  • Examples :
  • C Code
    # CALLOC #include <stdio.h> #include <stdlib.h> int main() { int i, n; int *element; printf("Enter total number of elements: "); scanf("%d", &n); element = (int*) calloc(n,sizeof(int)); if(element == NULL) { printf("Error.Not enough space available"); exit(0); } for(i=0;i<n;i++) { printf("Enter number :"); scanf("%d",element+i); } for(i=1;i<n;i++) { if(*element > *(element+i)) *element = *(element+i); } printf("Smallest element is %d",*element); return 0; }

    Output :
    Enter total number of elements: 3
    Enter number :1
    Enter number :2
    Enter number :3
    Smallest element is 1

    C Code
    # MALLOC #include <stdio.h> #include <stdlib.h> int main() { int n, i, *ptr, sum = 0; printf("Enter number of elements: "); scanf("%d", &n); ptr = (int*) malloc(n * sizeof(int)); if(ptr == NULL) { printf("Error! memory not allocated."); exit(0); } printf("Enter elements: "); for(i = 0; i < n; ++i) { scanf("%d", ptr + i); sum += *(ptr + i); } printf("Sum = %d", sum); free(ptr); return 0; }

    Output :
    Enter number of elements: 3
    Enter elements: 1 2 3
    Sum = 6

    C Code
    # REALLOC #include <stdio.h> #include <stdlib.h> int main() { int *ptr, i , n1, n2; printf("Enter size of array: "); scanf("%d", &n1); ptr = (int*) malloc(n1 * sizeof(int)); printf("Addresses of previously allocated memory: "); for(i = 0; i < n1; ++i) printf("%u\n",ptr + i); printf("\nEnter new size of array: "); scanf("%d", &n2); ptr = (int*)realloc(ptr, n2 * sizeof(int)); printf("Addresses of newly allocated memory: "); for(i = 0; i < n2; ++i) printf("%u\n", ptr + i); return 0; }

    Output :
    Enter size of array: 2
    Addresses of previously allocated memory: 12588000
    12588004

    Enter new size of array: 3
    Addresses of newly allocated memory: 12588000
    12588004
    12588008

  • Advantage :
  • - Allows flexible memory usage during program execution.
    - Useful for data structures whose size is not known at compile-time.
    - Helps manage memory efficiently for large or varying amounts of data.

  • Disadvantage :
  • - Requires explicit management (potential for memory leaks or dangling pointers if not handled properly).
    - Can lead to fragmentation of heap memory if allocations and deallocations are frequent and not properly managed.

  • Conclusion :
  • Dynamic memory allocation provides flexibility and efficiency in managing memory for data structures whose size or lifetime cannot be determined at compile time. However, it requires careful management to avoid memory leaks, fragmentation issues, and other pitfalls associated with manual memory management. Understanding these concepts is crucial for writing robust and reliable programs that efficiently use memory resources.