Re: realloc shrink memory



In article <d7d9c172-4fa7-4d7e-8984-7c9bfe0dfab2@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,
<wij@xxxxxxxxxxx> wrote:
Most realloc(void* ptr, size_t size) documents say that realloc
changes the size of ptr pointed memory block. But all the
following descriptions basically says the returned value is a
pointer to a new memory block.
My question is that if realloc is called to shorten the allocated
memory, is it always hold that the return value is the same as the
argument ptr?

No; realloc is allowed to allocate a new block (and move the contents
of the old block to the new block).

Otherwise, the first sentence is misleading.

It depends where you got that first sentence from; e.g. some Linux
manpage starts with:

"realloc() changes the size of the memory block pointed to by
ptr to size bytes."

and that's, in itself, a bit misleading. But the same manpage continues
with:

"realloc() returns a pointer to the newly allocated memory, which is
suitably aligned for any kind of variable and may be different from
ptr, or NULL if the request fails."

and the fact that it mentions "newly allocated memory" strongly suggests
that the block may have moved.

Here's what a few other documents say:

BSD manpage:
"The realloc() function changes the size of the object pointed to by ptr
to size bytes and returns a pointer to the (possibly moved) object."

Solaris manpage:
"The realloc() function changes the size of the block pointed
to by ptr to size bytes and returns a pointer to the (possi-
bly moved) block."

C89 standard:
"The realloc function changes the size of the object pointed to by
ptr to the size specified by size. [...] The realloc function returns
either a null pointer or a pointer to the possibly moved allocated space."

Dinkumware:
"void *realloc(void *ptr, size_t size);
The function allocates an object of size size, possibly obtaining
initial stored values from the object whose address is ptr. It returns
the address of the new object if successful; otherwise, it returns a
null pointer."

const size_t BSize=100;
void *p=malloc(BSize);
void *p1=realloc(p,BSize/2);
assert(p==p1); // assured?

Allowed: yes. Assured: no.
.



Relevant Pages

  • Re: realloc() implicit free() ?
    ... >If realloc() finds it necessary to move the memory block, ... The realloc function deallocates the old object pointed | ... The realloc function returns a pointer to the new | ... clear that if the memory block fgets moved the old memory is deallocated. ...
    (comp.lang.c)
  • Re: realloc limitations
    ... The reallocfunction returns a pointer to the newly allocated ... Thus ptr is no longer valid. ... "The realloc function deallocates the old object pointed to by ptr ... nor does it describe the deallocation as optional. ...
    (comp.lang.c)
  • Re: realloc limitations
    ... The reallocfunction returns a pointer to the newly allocated ... Thus ptr is no longer valid. ... "The realloc function deallocates the old object pointed to by ptr ... If the stored value of an object has such a representation ...
    (comp.lang.c)
  • Re: realloc limitations
    ... The reallocfunction returns a pointer to the newly allocated ... Thus ptr is no longer valid. ... "The realloc function deallocates the old object pointed to by ptr ... If the stored value of an object has such a representation ...
    (comp.lang.c)
  • Re: Typecasting pointers
    ... array of shorts, and initialize them all to the same value. ... You're saving the value of ptr so you can freeit later. ... A pointer increment advances the pointer by one object size, ... optimizing compiler could have done a better job than you have. ...
    (comp.lang.c)