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

"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."

"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. ...
  • 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. ...
  • Re: malloc realloc and pointers
    ... perticular position in a buffer then on performing a ... realloc() returns the same pointer, in which case ptr is still ... pointer is identical to the old pointer, you're not allowed to use them ...
  • RE: Multiple pointer entry`s for pc`s
    ... Pointer (PTR) RRs are used to support the reverse ... resolve this information to the DNS domain name for that computer. ... PTR RRs can be added to a zone in several ways: ...
  • Re: use delete to destroy primitive/object types but memory is not freed
    ... the safest approach is to think of a pointer as an opaque ... The call to mallocallocates memory space for a double object; ... We then assign a value to the double object that ptr points to, ... The C runtime system has reserved that chunk of memory, ...