Dynamic memory allocation in C allows for the resizing of arrays during runtime, helping to manage memory usage efficiently. Key functions include malloc(), calloc(), free(), and realloc(), which facilitate memory allocation and deallocation. This method contrasts with static memory allocation, which occurs at compile time and does not allow for memory size modification or reuse.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
7 views13 pages
Dynamic Memory Allocation
Dynamic memory allocation in C allows for the resizing of arrays during runtime, helping to manage memory usage efficiently. Key functions include malloc(), calloc(), free(), and realloc(), which facilitate memory allocation and deallocation. This method contrasts with static memory allocation, which occurs at compile time and does not allow for memory size modification or reuse.
● dynamically allocate a single large block of memory with the
specified size ● returns a pointer of type void
● ptr = (cast-type*) malloc(byte-size)
● Example : ptr = (int*) malloc(100 * sizeof(int)); Dynamic Memory Allocation : malloc() 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 { for (i = 0; i < n; ++i) { ptr[i] = i + 1; } } Dynamic Memory Allocation : calloc()
● Same as malloc() with two differences:
○ Initializes each block with a default 0 ○ Takes two parameters
● ptr = (cast-type*)calloc(n, element-size);
● Example : ptr = (float*) calloc(25, sizeof(float)); Dynamic Memory Allocation : calloc() int* ptr; int n, i; printf("Enter number of elements:"); scanf("%d",&n);
ptr = (int*)calloc(n, sizeof(int));
if (ptr == NULL) { printf("Memory not allocated.\n"); exit(0); } else { for (i = 0; i < n; ++i) { ptr[i] = i + 1; } } Dynamic Memory Allocation : free()
● dynamically de-allocate the memory so it can be reused
● free(ptr);
// heap overflow by continuously allocating memory
for (int i=0; i<10000000; i++)
{ // Allocating memory without freeing it int *ptr = (int *)malloc(sizeof(int)); } Dynamic Memory Allocation : malloc() and free() 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 { for (i = 0; i < n; ++i) { ptr[i] = i + 1; } } free(ptr); Dynamic Memory Allocation : realloc()
● dynamically re-allocate memory allocated by malloc(), calloc()
● maintains the already present value and new blocks will be initialized with the default garbage value.
● ptr = realloc(ptr, newSize);
Dynamic Memory Allocation : realloc() int* ptr; n = 10; int n, i; n = 5; ptr = (int*)realloc(ptr, n * ptr = (int*)calloc(n, sizeof(int)); sizeof(int)); if (ptr == NULL) { printf("Memory not for (i = 5; i < n; ++i) { allocated.\n"); ptr[i] = i + 1; exit(0); } } else { for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) { ptr[i] = i + 1; printf("%d, ", ptr[i]); } }
for (i = 0; i < n; ++i) { free(ptr);
printf("%d, ", ptr[i]); } } Dynamic Memory Allocation : for 2D Array int **array; printf("Enter the elements of the 2D int rows, cols, i, j; array:\n");
scanf("%d", &rows); for (i = 0; i < rows; i++) {
for (j = 0; j < cols; j++) { array = (int**) malloc(rows * sizeof(int*)); scanf("%d", &array[i][j]); if (array == NULL) { } printf("Memory allocation failed!"); } return -1; } printf("2D array elements:\n"); for (i = 0; i < rows; i++) { for (i = 0; i < rows; i++) { scanf("%d", &cols); for (j = 0; j < cols; j++) { array[i] = (int*) malloc(cols * printf("%d ", array[i][j]); sizeof(int)); } if (array[i] == NULL) { printf("\n"); printf("Memory allocation failed!"); } return -1; } } Dynamic VS Static memory allocation
Memory is allocated during program Memory is allocated during runtime. compilation. You cannot reuse allocated memory. You can reuse allocated memory after releasing memory using free() function. No library functions are required to Requires Library functions like malloc(), allocate memory. calloc(), realloc() to allocate memory at runtime. We cannot modify size of allocated We can modify size of allocated memory. memory using realloc().