[Bug c/17665] wrong code with -O2
jsm at polyomino dot org dot uk
gcc-bugzilla@gcc.gnu.org
Sat Sep 25 18:30:00 GMT 2004
------- Additional Comments From jsm at polyomino dot org dot uk 2004-09-25 18:30 -------
Subject: Re: wrong code with -O2
On Sat, 25 Sep 2004, davids at webmaster dot com wrote:
> I'll check the standard again, but my recollection is that it's supposed to be
> safe to cast any pointer to and from a 'void *'. That's why 'malloc'
> returns 'void *'.
The problematic dereference is not of a void * pointer, but of void ** and
char ** pointers pointing to the same place. You can use void * and
double * pointers (for example) pointing to the same place (though
dereferencing the void * pointer must involve casting it to char * or
double *). You can use int * and unsigned int * pointers pointing to the
same place. But you cannot use void ** and char ** pointers pointing to
the same place.
The *value* of a void * pointer can safely be converted to another pointer
type. What you are doing is treating the *representation* of that pointer
as being another type, via void ** and char ** pointers, and this is not
safe.
Read 6.5#7 again. Your object has one of void * and char * as effective
type, but its value is being accessed as the other, which is not
permitted: it is not within any of the listed cases. You need to access
the value as its effective type, then convert, rather than type punning
the representation.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=17665
More information about the Gcc-bugs
mailing list