This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: malloc attributes and realloc
On Fri, 2 Jan 2004, Gabriel Dos Reis wrote:
> void *p = malloc(34933);
> save_pointer_value_as_bits(p);
> p = realloc(7489483454);
> if (same_pointer_value_as_before(p))
> printf("realloc returned same pointer\n");
>
> That way, you can detect the behaviour of realloc.
If save_pointer_value_as_bits and same_pointer_value_as_before are
properly written (comparing with memcmp or as unsigned char, etc.) then
the taking of the addresses of the pointers *ought* to have the effect of
disabling optimisations about where they point to (even if we then
optimise the memcmp back to a comparison of the pointer values, it should
still have that effect). Whether this works, I don't know, but any bug
here is not realloc-specific (if you take the view of the proposed
committee response to DR#260 that objects with the same bit-pattern are
interchangable; if you don't, the fact that the memory has been freed and
reallocated may mean that even if the bit-patterns are the same, the
provenance of the old pointer makes it unusable).
--
Joseph S. Myers
jsm@polyomino.org.uk