dereferencing type-punned pointer will break strict-aliasing rules

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

i wan't to create aliase (symbolic name and explizit type) for the elements of an char-array. Unfortunately my gcc produce the warning "dereferencing type-punned pointer will break strict-aliasing rules", if i use -O2 or bigger. Since i set -Werror, the warning will become an error.

I attached an example to this mail. To compile and generate the problem, i use "gcc -O2 -Werror -Wall main.cpp".

I found an workaround (without using pragma, or somethink else), with it's included in the code, too. To activate, pass an extra "-DWorking" to the gcc-Command. The workaround is working, but using an pointer is ugly.

Best regrads,

Marcel

--
Dipl. Ing (FH) Marcel Behlau
(Software Developer)

ELFIN GmbH
Siegburger Straße 215
50679 Köln
Germany

Tel: +49 (221) 6778932-0
Fax: +49 (221) 6778932-2
marcel.behlau@xxxxxxxx
www.elfin.de


//gcc -O2 -Wall main.cpp
//gcc -O2 -Wall main.cpp -DWorking

//#define Working

struct Ec
{
  char ecData;
};

class ClassZero
{
public:
  char data[8];
  char* pointer;
  Ec& ec0;

#ifdef Working
  ClassZero() : pointer(data), ec0(*(Ec*)(pointer)) { }
#else
  ClassZero() : ec0(*(Ec*)(data)) { }
#endif

};

int main()
{
    return 0;
} 



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux