From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8332 invoked by alias); 7 Jun 2006 22:39:05 -0000 Received: (qmail 8296 invoked by uid 48); 7 Jun 2006 22:38:57 -0000 Date: Wed, 07 Jun 2006 22:43:00 -0000 Subject: [Bug c++/27941] New: offsetof produces possibly erroneous warnings X-Bugzilla-Reason: CC Message-ID: Reply-To: gcc-bugzilla@gcc.gnu.org To: gcc-bugs@gcc.gnu.org From: "casey dot shaar at pkinetics dot com" Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Archive: List-Post: List-Help: Sender: gcc-bugs-owner@gcc.gnu.org X-SW-Source: 2006-06/txt/msg00853.txt.bz2 List-Id: When working on an old code base, porting from Windows to Linux, using gcc version 4.1.0 20060304 (Red Hat 4.1.0-3) on my Fedora system, I got a series of warning from a header file which uses offsetof in several places - here's an example: PROM800.h: In member function ‘void ATPromInfo8C::UpdateVersion()’: PROM800.h:750: warning: invalid access to non-static data member ‘ATPromInfo8C::CRC’ of NULL object PROM800.h:750: warning: (perhaps the ‘offsetof’ macro was used incorrectly) Based on my understanding of offsetof, these warnings seemed unnecessary. After some experimentation, I discovered that this warning is only issued when offsetof is used with classes or structures which have a constructor. If a class or structure omits code for construction, no warning is issued. If code is included in the class or structure for construction, or just the declaration of constructors is included, then the above warning is issued. Below is a screen dump of the source code and the compile output with and without the constructor: /////////////////////////////////////////////////////////////////////////// #[cshaar@localhost PK76OTDR]$ more foo.cpp #include #include #include class goo { public: #if defined(BREAKIT) goo(int a); #endif int bar; }; struct foo { #if defined(BREAKIT) foo() { } #endif int bar; }; int main(int, char*[]) { size_t s = offsetof(foo,bar); size_t g = offsetof(goo,bar); return 0; } [cshaar@localhost PK76OTDR]$ cc foo.cpp [cshaar@localhost PK76OTDR]$ cc -DBREAKIT foo.cpp foo.cpp: In function ‘int main(int, char**)’: foo.cpp:26: warning: invalid access to non-static data member ‘foo::bar’ of NULL object foo.cpp:26: warning: (perhaps the ‘offsetof’ macro was used incorrectly) foo.cpp:27: warning: invalid access to non-static data member ‘goo::bar’ of NULL object foo.cpp:27: warning: (perhaps the ‘offsetof’ macro was used incorrectly) [cshaar@localhost PK76OTDR]$ /////////////////////////////////////////////////////////////////////////////// Thanks, Casey Shaar -- Summary: offsetof produces possibly erroneous warnings Product: gcc Version: 4.1.0 Status: UNCONFIRMED Severity: minor Priority: P3 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: casey dot shaar at pkinetics dot com http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27941