public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/27941]  New: offsetof produces possibly erroneous warnings
@ 2006-06-07 22:43 casey dot shaar at pkinetics dot com
  2006-06-07 22:44 ` [Bug c++/27941] " pinskia at gcc dot gnu dot org
  2008-06-08  1:41 ` tristan_schmelcher at alumni dot uwaterloo dot ca
  0 siblings, 2 replies; 3+ messages in thread
From: casey dot shaar at pkinetics dot com @ 2006-06-07 22:43 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2478 bytes --]

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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/27941] offsetof produces possibly erroneous warnings
  2006-06-07 22:43 [Bug c++/27941] New: offsetof produces possibly erroneous warnings casey dot shaar at pkinetics dot com
@ 2006-06-07 22:44 ` pinskia at gcc dot gnu dot org
  2008-06-08  1:41 ` tristan_schmelcher at alumni dot uwaterloo dot ca
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-06-07 22:44 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2006-06-07 22:43 -------
Both goo and foo are non PODs once you define a constructor which causes the
offsetof to be invalid.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27941


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/27941] offsetof produces possibly erroneous warnings
  2006-06-07 22:43 [Bug c++/27941] New: offsetof produces possibly erroneous warnings casey dot shaar at pkinetics dot com
  2006-06-07 22:44 ` [Bug c++/27941] " pinskia at gcc dot gnu dot org
@ 2008-06-08  1:41 ` tristan_schmelcher at alumni dot uwaterloo dot ca
  1 sibling, 0 replies; 3+ messages in thread
From: tristan_schmelcher at alumni dot uwaterloo dot ca @ 2008-06-08  1:41 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from tristan_schmelcher at alumni dot uwaterloo dot ca  2008-06-08 01:41 -------
Just a note for posterity:

Using offsetof in this particular case (and many others) will work in G++, but
the standard doesn't require that, so in theory it's not portable. That's why
G++ prints a warning. You can suppress it with -Wno-invalid-offsetof.


-- 

tristan_schmelcher at alumni dot uwaterloo dot ca changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tristan_schmelcher at alumni
                   |                            |dot uwaterloo dot ca


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=27941


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2008-06-08  1:41 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-07 22:43 [Bug c++/27941] New: offsetof produces possibly erroneous warnings casey dot shaar at pkinetics dot com
2006-06-07 22:44 ` [Bug c++/27941] " pinskia at gcc dot gnu dot org
2008-06-08  1:41 ` tristan_schmelcher at alumni dot uwaterloo dot ca

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).