diff options
| author | Vegard Nossum <vegard.nossum@gmail.com> | 2020-02-08 16:37:14 +0100 |
|---|---|---|
| committer | Michael Kerrisk <mtk.manpages@gmail.com> | 2020-02-08 23:37:10 +0100 |
| commit | 813d606b008d0a4c5bc5f6ac205c4e634a73fc21 (patch) | |
| tree | 8507d1d9ca8a549569e130f6918c663645960ed4 /man3 | |
| parent | f3aa51b217ff28380edae560c7129d3b7262ce36 (diff) | |
| download | man-pages-813d606b008d0a4c5bc5f6ac205c4e634a73fc21.tar.gz | |
malloc.3: realloc() return value
One might be tempted to think that realloc() always requests a new
allocation before moving the contents over (at least in the case
where the new size is bigger than the original). This is not the
case; for example, on my system the following program:
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
void *x = malloc(15);
void *y = malloc(32);
printf("x = %p\n", x);
printf("y = %p\n", y);
printf("usable_size(x) = %lu\n", malloc_usable_size(x));
void *z = realloc(x, 24);
printf("z = %p\n", z);
return 0;
}
prints:
x = 0x1b3a010
y = 0x1b3a030
usable_size(x) = 24
z = 0x1b3a010
Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
Diffstat (limited to 'man3')
| -rw-r--r-- | man3/malloc.3 | 9 |
1 files changed, 6 insertions, 3 deletions
diff --git a/man3/malloc.3 b/man3/malloc.3 index c3b688ab31..e0f5250ff7 100644 --- a/man3/malloc.3 +++ b/man3/malloc.3 @@ -216,9 +216,12 @@ function returns no value. The .BR realloc () function returns a pointer to the newly allocated memory, which is suitably -aligned for any built-in type and may be different from -.IR ptr , -or NULL if the request fails. +aligned for any built-in type, or NULL if the request failed. +The pointer may be the same as +.IR ptr +if the allocation was not moved (e.g. there was room to expand the allocation in-place), or different from +.IR ptr +if the allocation was moved to a new address. If .I size was equal to 0, either NULL or a pointer suitable to be passed to |
