C Library Function - realloc()
realloc() is a function in the C standard library used to reallocate memory space. It is very important in dynamic memory management, especially when the size of already-allocated memory needs to be adjusted.
C library function **void realloc(void ptr, size_t size) Attempts to resize the memory block previously allocated by a call to malloc or calloc and pointed to by ptr the given pointer.
Declaration
The following is the declaration of the realloc() function:
void *realloc(void *ptr, size_t size)
Parameters
- ptr -- A pointer to a memory block to be reallocated, which was previously allocated by calling malloc, calloc, or realloc. If it is a null pointer, a new memory block is allocated and the function returns a pointer to it.
- size -- The new size of the memory block, in bytes. If the size is 0 and ptr points to an existing memory block, the block pointed to by ptr is freed and a null pointer is returned.
Return Value
- On success,
realloc()Returns a pointer to the new memory block. - On failure, it returns
NULL, and the original memory block remains unchanged (it is not freed).
Usage notes
realloc()may move the memory block to a new location (if there is not enough space at the original location to accommodate the new size). If the move succeeds,ptrwill point to the new location. Note in particular that the oldptrpointer needs to be updated to therealloc()new address returned by.- If memory allocation fails,
realloc()ReturnNULL, and the original memory block is not freed. To avoid memory leaks, you should use a temporary pointer to receive therealloc()return value of, and check whether it isNULL.
Example
The following example demonstrates the use of the realloc() function.
Example 1
#include
<
stdio.h
#include
<
stdlib.h
#include
<
string.h
int
main
(
)
{
char
str;
/*
Initial memory allocation
*/
str
=
(
char
)
malloc
(
15
)
;
strcpy
(
str
,
"
runoob
"
)
;
printf
(
"
String = %s, Address = %p
\
n
"
,
str
,
str
)
;
/*
Reallocating memory
*/
str
=
(
char
)
realloc
(
str
,
25
)
;
strcat
(
str
,
"
.com
"
)
;
printf
(
"
String = %s, Address = %p
\
n
"
,
str
,
str
)
;
free
(
str
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
String = runoob, Address = 0x7fa2f8c02b10
String = runoob.com, Address = 0x7fa2f8c02b10
Example 2
#include
#include
int
main
(
)
{
int
arr
=
malloc
(
5
sizeof
(
int
)
)
;
// Allocate memory for 5 integers
if
(
arr
==
NULL
)
{
printf
(
"Memory allocation failed\n"
)
;
return
1
;
}
// Fill the array
for
(
int
i
=
0
;
i
<
5
;
i
++
)
{
arr
[
i
]
=
i
2
;
}
// Reallocate memory, resizing to 10 integers
int
new_arr
=
realloc
(
arr
,
10
sizeof
(
int
)
)
;
if
(
new_arr
==
NULL
)
{
printf
(
"Reallocation failed\n"
)
;
free
(
arr
)
;
// Free the original memory on failure
return
1
;
}
// Fill the new portion
for
(
int
i
=
5
;
i
<
10
;
i
++
)
{
new_arr
[
i
]
=
i
2
;
}
// Print the new array
for
(
int
i
=
0
;
i
<
10
;
i
++
)
{
printf
(
"%d "
,
new_arr
[
i
]
)
;
}
free
(
new_arr
)
;
// Finally, free the memory
return
0
;
}
Notes
- Check the return value: always check
realloc()the return value of. If it returnsNULL, do not directly discard the original pointer. - Memory Leaks: If
realloc()ReturnNULL, the original memory block is not freed. In this case, you should free the original memory and handle the error condition. - Pointer invalidation: If you directly use the original pointer to receive the
realloc()new pointer returned by, it may cause a memory leak, especially whenrealloc()fails. Therefore, it is common to first receive the return value with a temporary pointer.
Common Use Cases
- Dynamically expand the size of arrays or other data structures.
- Used to handle variable-sized datasets at runtime, such as dynamically generated lists, strings, or caches.
C Standard Library -