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

Re: warning: invalid offsetof from non-POD type


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


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