warning: invalid offsetof from non-POD type

John Quigley johnw@lowestplane.org
Sat Mar 29 19:27:00 GMT 2003


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



More information about the Gcc-help mailing list