Welcome to the exciting world of dynamic memory allocation in C! In this lesson, we'll dive deep into understanding how to manage memory dynamically in your C programs, making them more flexible and efficient. Let's get started! 🚀
Dynamic memory allocation refers to the process of requesting and releasing memory during the runtime of a program. Unlike static memory allocation, where memory is allocated at compile time, dynamic memory allocation allows us to allocate and deallocate memory at runtime, which is crucial for developing flexible and dynamic programs.
malloc(): A function used to dynamically allocate memory.calloc(): A function used to dynamically allocate memory and initialize it to zero.free(): A function used to deallocate dynamically allocated memory.realloc(): A function used to change the size of a dynamically allocated block of memory.Before we dive into examples, let's first cover some important concepts:
In C, there are various data types, some of which are:
int: A whole numberfloat: A floating-point numberchar: A charactervoid: A data type that represents the absence of any dataNow that we've covered the basics, let's see how to use malloc() to dynamically allocate memory.
void *malloc(size_t size);size_t is an unsigned integer type that represents the size of the memory to be allocated.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *array;
int size = 10;
array = (int *)malloc(size * sizeof(int));
if (array == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Use the allocated memory
array[0] = 10;
array[1] = 20;
array[2] = 30;
// Print the contents of the array
for (int i = 0; i < size; i++) {
printf("array[%d] = %d\n", i, array[i]);
}
// Free the allocated memory
free(array);
return 0;
}In this example, we dynamically allocate an array of ints with 10 elements using malloc(). We check if the memory allocation was successful by checking if the returned pointer is NULL. We then use the allocated memory and, once we're done, we free the memory using free().
calloc() is similar to malloc(), but it initializes the allocated memory to zero.
void *calloc(size_t number_of_elements, size_t size_of_each_element);#include <stdio.h>
#include <stdlib.h>
int main() {
int *array;
int size = 10;
array = calloc(size, sizeof(int));
if (array == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Use the allocated and initialized memory
array[0] = 10;
array[1] = 20;
array[2] = 30;
// Print the contents of the array
for (int i = 0; i < size; i++) {
printf("array[%d] = %d\n", i, array[i]);
}
// Free the allocated memory
free(array);
return 0;
}In this example, we dynamically allocate and initialize an array of ints with 10 elements using calloc(). We then use the allocated and initialized memory as in the previous example.
Once you're done with the dynamically allocated memory, it's essential to free the memory to avoid memory leaks.
void free(void *pointer);pointer is the pointer to the dynamically allocated memory that we want to deallocate.
realloc() allows you to change the size of a dynamically allocated block of memory.
void *realloc(void *pointer, size_t new_size);pointer is the pointer to the previously allocated memory, and new_size is the new size of the memory block.
#include <stdio.h>
#include <stdlib.h>
int main() {
int *array;
int size = 10;
array = malloc(size * sizeof(int));
if (array == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Use the allocated memory
array[0] = 10;
array[1] = 20;
array[2] = 30;
// Increase the size of the array
size *= 2;
array = realloc(array, size * sizeof(int));
if (array == NULL) {
printf("Memory reallocation failed.\n");
return 1;
}
// Add more elements to the array
array[3] = 40;
array[4] = 50;
// Print the contents of the array
for (int i = 0; i < size; i++) {
printf("array[%d] = %d\n", i, array[i]);
}
// Free the allocated memory
free(array);
return 0;
}In this example, we first allocate an array of ints with 10 elements using malloc(). We then increase the size of the array using realloc() and add more elements to the array.
Which function initializes the dynamically allocated memory to zero?
That's it for this lesson! In the next lesson, we'll explore strings and character handling in C. Until then, happy coding! 🎉