This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
invalid offsetof from non-POD type
- From: John Quigley <johnw at lowestplane dot org>
- To: gcc at gcc dot gnu dot org
- Date: Sun, 20 Apr 2003 22:11:03 -0700
- Subject: invalid offsetof from non-POD type
The code below triggers an "invalid offsetof from non-POD type" warning:
In this case, and in every case I've seen, the warning is a false alarm and
the compiler produces the correct result. Is there a case where the warning
fires and the compiler produces an incorrect result? (i.e.: returns the
wrong offset)? If there is no such case, is there anything wrong with
providing a command line flag in gcc that allows the user to disable this
warning?
Thanks,
- jq
#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 be assigning
*yptr = 15;
cout << "foo values: " << foo.x << ", " << foo.y << endl;
return 0;
}