Realloc function in C
The realloc() function in C is used to dynamically resize a block of memory that was previously allocated using malloc(), calloc(), or realloc() functions. The function takes two arguments: a pointer to the block of memory to be resized, and the new size of the block in bytes.
Syntax:
The syntax for the realloc() function is as follows:
void *realloc(void *ptr, size_t size);
If ptr is NULL, then the realloc() function behaves like malloc() and allocates a new block of memory of the given size.
It is important to note that the contents of the newly allocated block of memory may be different from the contents of the original block of memory, depending on the new size of the block and the alignment requirements of the system. Therefore, it's important to copy the contents of the original block to the newly allocated block if necessary.
Parameters of C realloc() Function
The realloc() function in C takes two parameters:
- ptr: This is a pointer to the memory block that was previously allocated using malloc(), calloc(), or realloc(). If ptr is NULL, then realloc() behaves like malloc() and allocates a new block of memory of the given size.
- size: This is the new size of the memory block, in bytes. If size is 0, then realloc() frees the memory block pointed to by ptr.
Exceptions of C realloc() Function
The realloc() function in C can raise two types of exceptions:
- If realloc() fails to allocate the requested memory, it returns NULL. This can happen if there is not enough memory available in the system to fulfil the request. In this case, the original block of memory pointed to by ptr remains unchanged.
- If the ptr argument passed to realloc() is not a valid pointer to a block of memory previously allocated with malloc(), calloc(), or realloc(), the behavior of realloc() is undefined. This can lead to a program crash or other unexpected behavior.
Therefore, it is important to always check the return value of realloc() and ensure that the ptr argument passed to it is a valid pointer to a block of memory previously allocated with malloc(), calloc(), or realloc(). It's also important to handle the case where realloc() returns NULL and take appropriate action, such as freeing other resources and terminating the program.
Here is an example of using the realloc() function in C to resize a block of memory:
Example:
#include <stdio.h>
#include <stdlib.h>
int main() {
// Allocate an initial block of memory
int *p = malloc(5 * sizeof(int));
if (p == NULL) {
printf("Failed to allocate memory\n");
return 1;
}
// Initialize the memory block
for (int i = 0; i< 5; i++) {
p[i] = i * 2;
printf("%d ", p[i]);
}
printf("\n");
// Resize the memory block to hold 10 integers
int *q = realloc(p, 10 * sizeof(int));
if (q == NULL) {
printf("Failed to reallocate memory\n");
free(p);
return 1;
}
// Initialize the new elements of the memory block
for (int i = 5; i< 10; i++) {
q[i] = i * 2;
}
// Print the contents of the resized memory block
for (int i = 0; i< 10; i++) {
printf("%d ", q[i]);
}
printf("\n");
// Free the memory block
free(q);
return 0;
}
Output:
0 2 4 6 8
0 2 4 6 8 10 12 14 16 18
In this example, we first allocate a block of memory using malloc() to hold 5 integers. We then initialize the memory block with even numbers and print them to the console.
We then use realloc() to resize the memory block to hold 10 integers. If realloc() fails to allocate the requested memory, we free the original block of memory using free() and terminate the program. We then initialize the new elements of the memory block with even numbers and print the entire block to the console.
Finally, we free the memory block using free() to release the memory back to the system.
Use of realloc() function in C
The realloc() function in C is a powerful tool for managing memory dynamically. Some common uses of realloc() include:
- Resizing an array: The realloc() function can be used to resize an array dynamically by increasing or decreasing its size. This is useful when the size of the array is not known in advance or changes during program execution.
- Implementing dynamic data structures: The realloc() function can be used to implement dynamic data structures such as linked lists, stacks, and queues, where the size of the structure can change dynamically as new elements are added or removed.
- Reducing memory waste: The realloc() function can be used to reduce memory waste by reallocating memory blocks to the minimum size required to hold the data they contain. This can help to optimize memory usage in programs that manipulate large data structures or handle large amounts of data.
- Avoiding unnecessary copying: The realloc() function can be used to avoid unnecessary copying of data when resizing a memory block. Instead of copying the contents of the old block to a new block, realloc() can resize the old block in place if there is enough space available.
Overall, the realloc() function provides a flexible and efficient way to manage memory dynamically in C programs. However, it is important to use realloc() carefully and always check the return value to avoid unexpected behavior or crashes.