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




Herbert Rosenau <os2guy@xxxxxxxxxxxxx> wrote in message
news:wmzsGguTDN6N-pn2-KdbpSkdEwzDo@xxxxxxxxxxxxxxxxxxxxxxxxx
On Sun, 13 Aug 2006 16:14:19 UTC, "Bill Reid"
<hormelfree@xxxxxxxxxxxxxxxx> wrote:

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

void *p = NULL; /* we have no memory yet */
void *temp; /* realloc will set it */

size_t size = 0; /* we calculate the size in the loop before we call
realloc */
...
for (....) {
....
if ((temp = realloc(p, size) != NULL) {
/* realloc failed */
return NULL; /* or some other error code */
}
p = temp;
....
}
free(p);

will work always - except your implementation is really broken. But
hten trow your compiler into trash and get another one.

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

I would say you have forgotten to initialise the pointer given to
realloc with NULL signalling it that thre is currently nothing to
realloc but malloc.

You are correct sir!

I made yet a further fool of myself and posted the question on
the message board for the compiler. Sure enough, I "forgot" that
local pointers are not initialized, therefore are not NULL and
could be anything, so realloc() tries to allocate memory in
god-knows-where.

Of course, I could have sworn that I explicitly set the pointer
to NULL as the FIRST thing I tried to fix the problem when
it first cropped up years ago, but I must have mucked that up
somehow.

As to your code, I'm not sure why you do the two-step
with "*p" and "*temp"...it seems like all is required is to set
*p=NULL when declared, at least that's what I did, and
it worked just fine. Am I (again!) missing something?

---
William Ernest Reid



.



Relevant Pages

  • Re: Can I Trust Pointer Arithmetic In Re-Allocated Memory?
    ... If your compiler ... it works with a cast. ... Pointer arithmetic, as you probably know, is scaled by ... not sure about concerning realloc(). ...
    (comp.lang.c)
  • Re: trouble in freeing memory using realloc
    ... > function to which memory is allocated using malloc and reallocated ... > address to which the pointer is pointing is changed during one of the ... > realloc calls. ... Is it dependent on the compiler (the IDE is MS Visual ...
    (comp.lang.c)
  • Re: Modifying pointers of unknown type via a function call
    ... If you want to do realloc type stuff, do it the way realloc does it. ... example to pass in a pointer to a pointer. ... but although all pointers are freely castable to and from void*, ... One compiler I use allows &a, ...
    (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)
  • Re: Dynamic buffer library
    ... We do it this way instead of using a struct so that ... /* We now need to get the address of the buffer, because realloc() ... The address of the _pointer_. ... from and to all data pointer types, but void** canNOT validly point to ...
    (comp.lang.c)