[Bug c++/27941] New: offsetof produces possibly erroneous warnings
casey dot shaar at pkinetics dot com
gcc-bugzilla@gcc.gnu.org
Wed Jun 7 22:43:00 GMT 2006
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
More information about the Gcc-bugs
mailing list