Re: resizing an array, is there a better way?
From: Keith Thompson (kst-u_at_mib.org)
Date: 08/05/04
- Next message: Keith Thompson: "Re: strange error message : unable to find a register to spill in class `AREG'"
- Previous message: Keith Thompson: "Re: Null pointers"
- In reply to: Jens.Toerring_at_physik.fu-berlin.de: "Re: resizing an array, is there a better way?"
- Next in thread: CBFalconer: "Re: resizing an array, is there a better way?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 05 Aug 2004 01:12:58 GMT
Jens.Toerring@physik.fu-berlin.de writes:
> Keith Thompson <kst-u@mib.org> wrote:
[...]
> > All this is, of course, extremely system-specific. Michael is quite
> > correct: you *don't know* what realloc does.
>
> System-specific or not, would you be prepared to tell which implemen-
> tations you were testing that with?
Cygwin under Windows XP and Solaris 8. On Cygwin, the behavior was
unaffected by the extra malloc() call; on Solaris, it caused the
realloc() to move the block more often.
Actually, I should have stated it more strongly. It's not just
system-specific; it can depend strongly on the current state of the
heap (assuming there is such a thing), which can depend on what else
the program has been doing, and quite possibly on the phase of the
moon.
[...]
> > Here's the program. One caveat: the "ptr != prev" comparison probably
> > invokes undefined behavior when realloc() moves the memory block and
> > ptr becomes invalid.
>
> As far as I understand the constraints, comparing pointers for equality
> or inequality is absolutely ok, only trying to establish a relation like
> that one is larger than the other gets you into muddy waters.
If the realloc() call moves the block of memory, the old pointer value
becomes invalid, just like a pointer that's been free()d.. Any
attempt to refer to that value will invoke undefined behavior. In
nearly all real-world implementations, an equality comparison will
just compare the bits, and it will work as expected -- but an
implementation could, for example, load the value into an address
register for the comparison, and this could trigger a validity check.
-- Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this.
- Next message: Keith Thompson: "Re: strange error message : unable to find a register to spill in class `AREG'"
- Previous message: Keith Thompson: "Re: Null pointers"
- In reply to: Jens.Toerring_at_physik.fu-berlin.de: "Re: resizing an array, is there a better way?"
- Next in thread: CBFalconer: "Re: resizing an array, is there a better way?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|