Ticker

6/recent/ticker-posts

Dynamic Memory Allocation

 Chapter 12: Dynamic Memory
Allocation



 




Dynamic memory allocation in C allows you to
allocate memory at runtime, giving you more flexibility in managing memory for
your programs. In this chapter, we will explore how to allocate and deallocate
memory dynamically using the `malloc()`, `calloc()`, and `free()` functions:



 



12.1
malloc() Function:



The `malloc()` function is used to dynamically
allocate memory in C. It takes the size in bytes as an argument and returns a
pointer to the allocated memory block.



 





void*
malloc(size_t size);





 



Example:



 





#include
<stdio.h>



#include
<stdlib.h>



int
main() {



    int* numbers;



    int size;



 



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



    scanf("%d", &size);



 



    numbers = (int*)malloc(size * sizeof(int));



 



    if (numbers == NULL) {



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



        return 1;



    }



 



    printf("Memory allocated
successfully.\n");



 



    //
Use the allocated memory



 



    free(numbers);



 



    return 0;



}





 



The program dynamically allocates an array of
integers based on the user input size using `malloc()`. It checks if the memory
allocation was successful by checking if the returned pointer `numbers` is
NULL. Finally, it frees the allocated memory using `free()`.



 



12.2
calloc() Function:



The `calloc()` function is used to dynamically
allocate memory in C and initialize the allocated memory to zero. It takes the
number of elements and the size of each element as arguments and returns a
pointer to the allocated memory block.



 





void*
calloc(size_t num, size_t size);





 



Example:



 





#include
<stdio.h>



#include
<stdlib.h>



int
main() {



    int* numbers;



    int size;



 



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



    scanf("%d", &size);



 



    numbers = (int*)calloc(size, sizeof(int));



 



    if (numbers == NULL) {



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



        return 1;



    }



 



    printf("Memory allocated
successfully.\n");



 



    // Use the allocated memory



 



    free(numbers);



 



    return 0;



}





The program dynamically allocates an array of
integers and initializes the memory to zero using `calloc()`. The process of
checking for successful allocation and freeing the memory remains the same as
in the `malloc()` example.



 



12.3
free() Function:



The `free()` function is used to deallocate the
memory previously allocated using `malloc()` or `calloc()`. It takes the
pointer to the allocated memory as an argument.



 





void
free(void* ptr);





 



Example:





#include
<stdio.h>



#include
<stdlib.h>



int
main() {



    int* number;



 



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



 



    if (number == NULL) {



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



        return 1;



    }



 



    *number = 42;



 



    printf("Number: %d\n", *number);



 



    free(number);



 



    return 0;



}





 



The program dynamically allocates memory for an
integer, assigns a value to it, and prints the value. Finally, it frees the
allocated memory using `free()`.



 



12.4
realloc() Function:



The `realloc()` function is used to resize the
previously allocated memory block. It takes the pointer to the existing memory
block and the new size in bytes as arguments and returns a pointer to the
resized memory block.



 





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





 



Example:



 





#include
<stdio.h>



#include
<stdlib.h>



int
main() {



    int* numbers;



    int size;



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



    scanf("%d", &size);



 



    numbers = (int*)malloc(size * sizeof(int));



 



    if (numbers == NULL) {



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



        return 1;



    }



 



    printf("Memory allocated
successfully.\n");



 



    // Resize the array



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



    scanf("%d", &size);



 



    numbers = (int*)realloc(numbers, size *
sizeof(int));



 



    if (numbers == NULL) {



        printf("Memory reallocation
failed.\n");



        return 1;



    }



 



    printf("Memory reallocated
successfully.\n");



 



    // Use the resized array



 



    free(numbers);



 



    return 0;



}





 



The program dynamically allocates an array of
integers based on the user input size using `malloc()`. It then asks the user
to enter the new size and uses `realloc()` to resize the array accordingly. The
process of checking for successful allocation and freeing the memory remains
the same as in the previous examples.







 

Post a Comment

0 Comments