Re: malloc realloc and pointers



On Thu, 29 Nov 2007 17:00:05 -0500, Kenneth Brody wrote:
ravi wrote:
2. With realloc(), if some pointer 'ptr' is pointing initially to a
perticular position in a buffer (char *buffer) then on performing a
realloc() on this buffer, what will be 'ptr' pointing to?

Three possibilities:

1) realloc() returns the same pointer, in which case ptr is still
valid, pointing to the newly-sized buffer.

According to DR #260, applied to realloc, even if memcmp proves the new
pointer is identical to the old pointer, you're not allowed to use them
interchangeably. If you pass a non-null pointer ptr to realloc, and it
doesn't return a null pointer, ptr is no longer valid.

(I do not believe the DR's conclusions can be drawn from any
interpretation of the actual standard, and in fact I believe some points
from the DR directly contradict explicit guarantees by the standard, but
that's just me.)
.



Relevant Pages

  • Re: Kernel malloc/realloc?
    ... one of the semantics of realloc is that the same pointer as before can be ... make sure any threads that are using the previous buffer have stopped ... >> if you need to have a bigger allocation ...
    (microsoft.public.development.device.drivers)
  • Re: Kernel malloc/realloc?
    ... > one of the semantics of realloc is that the same pointer as before can be ... Any for of realocation of a memory buffer can't possible guarantee to return ... I just don't know how to acctualy resize a memory buffer in kernel mode. ...
    (microsoft.public.development.device.drivers)
  • 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 ... pointing to the newly-sized buffer. ...
    (comp.lang.c)
  • Re: Reading long lines from a file
    ... can you have a pointer of equal size to a buffer except by sizing the buffer to be the same size as a pointer. ... Once we get those impossibilities out of the way, we can dispense with the unnecessary fgets call - your input is already buffered, so why buffer it again through fgets? ... Since we do not know what p points to we cannot say whether you are allowed to realloc what it points to or not. ...
    (comp.lang.c)
  • Re: free memory
    ... There's also calloc and realloc to consider. ... If ptr is a null pointer, ... Even if reallocdeallocates the memory at ptr and with 0 as a size argument, if it were successful it returns a pointer to memory of zero length? ...
    (comp.lang.c)