This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: void* <-> char* aliasing rule, C standard or glitch?
- From: Andrew Haley <aph at redhat dot com>
- To: Elmar Krieger <elmar at cmbi dot ru dot nl>
- Cc: gcc at gcc dot gnu dot org
- Date: Sat, 1 Jul 2006 19:52:12 +0100
- Subject: Re: void* <-> char* aliasing rule, C standard or glitch?
- References: <44A6C3A1.9010306@cmbi.ru.nl>
Elmar Krieger writes:
>
> I searched hard, but couldn't determine conclusively if the C standard
> allows to alias a void* pointer with a char* pointer.
>
> If that's not undefined behavior, then the following may be a glitch in
> GCC 4.1.0 when compiled with -O2.
>
> Here's the ugly minimal piece of code:
>
> /* ASSUME pointer POINTS TO AN INTEGER THAT SPECIFIES THE INCREMENT
> ================================================================ */
> int increment(int *pointer)
> { return(*pointer); }
>
> /* INCREMENT A POINTER BY THE NUMBER OF BYTES POINTED TO
> ===================================================== */
> void *incremented(void *pointer)
> { int inc=increment(pointer);
> *((char**)&pointer)+=inc;
Not legal C. Do
pointer = (char*)pointer + inc;
Andrew.