Shrinking with realloc
david.f.prosser
dfp at cbnewsl.ATT.COM
Fri Aug 11 01:35:35 AEST 1989
In article <26328 at shemp.CS.UCLA.EDU> signup at CS.UCLA.EDU writes:
>If realloc is used to shrink a chunk of allocated storage, is it guaranteed
>by the standard that the storage won't be moved? I couldn't find any positive
>or negative statement about the matter.
>
>I.e., after
> char *p, *q;
> q = p = (char *) malloc(10);
> (void) realloc(p,5);
>is it valid to dereference q?
Short answer: "no".
Long answer: Having such a requirement on realloc would overly constrain
its implementation, quite possibly causing wasted memory. This is because
the fancier malloc/realloc/free implementations have special pools for
certain sized (typically small) blocks, and crossing such a size boundary
would necessitate copying the data. Also, copying of a small amount of
data is not too costly when the overall space usage "wins".
Dave Prosser ...not an official X3J11 answer...
More information about the Comp.std.c
mailing list