Mail Archives: djgpp/1997/08/04/15:33:59
Shawn Hargreaves (Shawn AT talula DOT demon DOT co DOT uk) writes:
> I'm not sure about the gcc malloc implementation, but I have in the past
> used compilers that did move the block around (it took me forever to
> work out why my code suddenly stopped working when I changed compiler!).
> It's probably not a good plan to assume that it will stay in the same
> place...
It is always a good idea to do one of two things with stuff you intend to
realloc:
1. Only use the one pointer to it; regenerate that pointer from realloc's
return value.
Or if this is impractical:
2. Double indirection. Malloc or calloc to a whatever * pointer. Assign
this pointer's address to a whatever ** pointer. Use that pointer
everywhere, using (*(*pointer)) to access data. When realloc is used,
assign its return value into the whatever * pointer; thus what is pointed
to by all copies of the doubly indirect pointer will be the new address.
--
.*. Where feelings are concerned, answers are rarely simple [GeneDeWeese]
-() < When I go to the theater, I always go straight to the "bag and mix"
`*' bulk candy section...because variety is the spice of life... [me]
Paul Derbyshire ao950 AT freenet DOT carleton DOT ca, http://chat.carleton.ca/~pderbysh
- Raw text -