This is the mail archive of the gcc@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: void* <-> char* aliasing rule, C standard or glitch?


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.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]