public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* warning: invalid offsetof from non-POD type
@ 2003-03-28 17:52 John Quigley
  2003-03-28 17:58 ` Eljay Love-Jensen
  0 siblings, 1 reply; 4+ messages in thread
From: John Quigley @ 2003-03-28 17:52 UTC (permalink / raw)
  To: gcc-help

Hello,

I maintain the GNU/Linux port of a large c++ system.  When compiling it with 
gcc3, I get hundreds of lines of warnings about "invalid offsetof from 
non-POD type, use pointer to method instead".  Despite these many warnings, 
the executable works fine.  

The system uses an "offsetof"-like macro to bind c++ member fields to its 
internal scripting language.  It is not a simple change to replace this 
offsetof scheme with a pointer to member.  

Can someone give me an example of code where the warning would fire and the 
resulting code would give incorrect results? 

Thanks,
- jq

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

* Re: warning: invalid offsetof from non-POD type
  2003-03-28 17:52 warning: invalid offsetof from non-POD type John Quigley
@ 2003-03-28 17:58 ` Eljay Love-Jensen
  2003-03-29 19:27   ` John Quigley
  0 siblings, 1 reply; 4+ messages in thread
From: Eljay Love-Jensen @ 2003-03-28 17:58 UTC (permalink / raw)
  To: John Quigley, gcc-help

Hi John,

Could you send a compilable code snippet that generates the error.

Thanks,
--Eljay

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

* Re: warning: invalid offsetof from non-POD type
  2003-03-28 17:58 ` Eljay Love-Jensen
@ 2003-03-29 19:27   ` John Quigley
  2003-03-30  1:45     ` Oscar Fuentes
  0 siblings, 1 reply; 4+ messages in thread
From: John Quigley @ 2003-03-29 19:27 UTC (permalink / raw)
  To: Eljay Love-Jensen, gcc-help

On Friday 28 March 2003 9:52 am, Eljay Love-Jensen wrote:
> Hi John,
>
> Could you send a compilable code snippet that generates the error.
>
> Thanks,
> --Eljay

Here you go.  Note that it is a warning, not an error.  

// BEGIN CODE
#include <cstddef>
#include <iostream>
using namespace std;

class Foo
{
   public:
      int x;
      char fillerdata[256];
      int y;

      Foo()
      {
         x = 0;
         y = 0;
      }
};

int main()
{
   int yoffset = offsetof(Foo, y); // triggers warning
   cout << "yoffset: " << yoffset << endl;

   Foo foo;
   
   // get a pointer to y using the computed offset
   int* yptr = (int*)(((unsigned char*)&foo) + yoffset);
   // test it by assigning value
   *yptr = 15;
   cout << "foo values: " << foo.x << ", " << foo.y << endl;

   return 0;
}
// END CODE

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

* Re: warning: invalid offsetof from non-POD type
  2003-03-29 19:27   ` John Quigley
@ 2003-03-30  1:45     ` Oscar Fuentes
  0 siblings, 0 replies; 4+ messages in thread
From: Oscar Fuentes @ 2003-03-30  1:45 UTC (permalink / raw)
  To: gcc-help

John Quigley <johnw@lowestplane.org> writes:

> Here you go.  Note that it is a warning, not an error.  
>
> // BEGIN CODE
> #include <cstddef>
> #include <iostream>
> using namespace std;
>
> class Foo
> {
>    public:
>       int x;
>       char fillerdata[256];
>       int y;
>
>       Foo()
>       {
>          x = 0;
>          y = 0;
>       }
> };
>
> int main()
> {
>    int yoffset = offsetof(Foo, y); // triggers warning
>    cout << "yoffset: " << yoffset << endl;
>
>    Foo foo;
>    
>    // get a pointer to y using the computed offset
>    int* yptr = (int*)(((unsigned char*)&foo) + yoffset);
>    // test it by assigning value
>    *yptr = 15;
>    cout << "foo values: " << foo.x << ", " << foo.y << endl;
>
>    return 0;
> }
> // END CODE

The C++ Standard says that offsetof is only required to work for POD
types. The warning is remembering that to you.

-- 
Oscar

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

end of thread, other threads:[~2003-03-29 19:27 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-28 17:52 warning: invalid offsetof from non-POD type John Quigley
2003-03-28 17:58 ` Eljay Love-Jensen
2003-03-29 19:27   ` John Quigley
2003-03-30  1:45     ` Oscar Fuentes

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).