This is the mail archive of the gcc-bugs@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]

[Bug c++/27941] New: offsetof produces possibly erroneous warnings


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 <stdlib.h>
#include <stdio.h>
#include <stddef.h>

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


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