Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?




Bill Reid <hormelfree@xxxxxxxxxxxxxxxx> wrote in message
news:uTtDg.241794$mF2.19376@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Keith Thompson <kst-u@xxxxxxx> wrote in message
news:ln64gyvgtc.fsf@xxxxxxxxxxxxxxxxxx
"Bill Reid" <hormelfree@xxxxxxxxxxxxxxxx> writes:

Oh, while I've got you here, here's another issue I noticed that I'm
not sure about concerning realloc(). Here's the NON-documentation:

Syntax

#include <stdlib.h>
void *realloc(void *block, size_t size);

...

If block is a NULL pointer, realloc works just like malloc.

...

I read this years ago, and thought "Great, I don't necessarily have to
malloc something first, I can use realloc in a loop and the first pass
through the loop it'll just be like malloc."

Yes. If it doesn't work that way, your implementation is broken.
(But that's an unlikely bug, since the behavior is clearly documented
in the standard.)

I would think it unlikely it is broken, the package is irritatingly bad
in many ways but seems to generally put out clean functioning programs
after fighting the "tools", but who knows. I may have just done something
stupid, wouldn't be the first time...

Problem is, it didn't seem to work out that way, and I'm not sure
what I did wrong, but I think I tried a number of things, such as
explicitly initializing my memory pointer to NULL, and always got
an error...is it actually possible to use realloc() to act like malloc
with a NULL pointer?

Yes. I can't guess why you were unable to get it to work.

Maybe I'll try it again.

Oooooh, that was gnarly...

What I forgot was that if I don't malloc() the block first, if I
realloc() in a loop I get a memory access exception. I hate it
when that happens...

Maybe it IS a bug in the compiler, if it wasn't so easy to work
around, I might actually worry about it more. As it is, I did a
search on the compiler maker's web-site for any information
on known bugs, came up with nothing, and left a question on
the discussion forum about it, see if anybody knows anything...

---
William Ernest Reid



.



Relevant Pages

  • Re: Extra bytes
    ... malloc, assigning a new block of size bytes and returning a pointer to ... "The realloc function changes the size of an allocated memory block. ... The memblock argument points to the beginning of the memory block. ...
    (comp.unix.programmer)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... I believe I actually do call malloc() in some xxx.cpp files... ... What you quoted above is not *the* documentation for qsort. ... If block is a NULL pointer, realloc works just like malloc. ...
    (comp.lang.c)
  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... I believe I actually do call malloc() in some xxx.cpp files... ... If block is a NULL pointer, realloc works just like malloc. ... I can use realloc in a loop and the first pass ...
    (comp.lang.c)
  • Re: Question about the *= (and similar) operator
    ... always return a pointer to fresh storage. ... calling realloc. ... Assume malloc works and then realloc fails. ... We'll have ptr set to NULL and no reference to the memory malloc gave us. ...
    (comp.lang.c)
  • Re: realloc() implicit free() ?
    ... >> another] what happens to the input block when realloc() finds it ... > you must instead use the output pointer value. ... The realloc function changes the size of the object pointed to by ... new, deallocate the old, and return a pointer to the new -- but I see ...
    (comp.lang.c)