P
Pawel
Hallo group members.
//p1.cpp
#include <stdio.h>
#include <linux/stddef.h>
struct Person {
int m_age;
char* m_name;
};
struct Child: public Person {
unsigned short m_school_year;
};
int main()
{
printf ("offset to m_name: %d\n", offsetof(Child, m_name));
printf ("offset to m_length: %d\n", offsetof(Child, m_school_year));
}
As You know the program above compiles with a warning:
prompt$ g++ -g -O0 -o /tmp/p1 p1.cpp
p1.cpp: In function `int main()':
p1.cpp:16: warning: invalid access to non-static data member `Person::m_name' of NULL object
p1.cpp:16: warning: (perhaps the `offsetof' macro was used incorrectly)
p1.cpp:17: warning: invalid access to non-static data member `Child::m_school_year' of NULL object
p1.cpp:17: warning: (perhaps the `offsetof' macro was used incorrectly)
As I read in this newsgroup, the reason is casting 0 to address non-POD structure.
Indeed, the following code produces the same warning:
int main()
{
#define BASE 0x0
printf ( "m_name: %d\n", &((Child*)BASE)->m_school_year );
}
My question is: can I safely ignore this warning for structures of the complexity above?
Again, I found in this newsgroup that solution would be just to turn off this warning
with -Wno-invalid-offsetof flag.
Another solution is, if BASE != 0, then warning is not produced, so that would be the solution.
offsetof macro would look like following:
#define OFFSETOF_BASE 0x1
#define Offsetof(TYPE, MEMBER) \
(size_t)((unsigned char*)(&(((TYPE*)OFFSETOF_BASE)->MEMBER)) - (unsigned char*)(TYPE*)OFFSETOF_BASE)
Regards,
Pawel
//p1.cpp
#include <stdio.h>
#include <linux/stddef.h>
struct Person {
int m_age;
char* m_name;
};
struct Child: public Person {
unsigned short m_school_year;
};
int main()
{
printf ("offset to m_name: %d\n", offsetof(Child, m_name));
printf ("offset to m_length: %d\n", offsetof(Child, m_school_year));
}
As You know the program above compiles with a warning:
prompt$ g++ -g -O0 -o /tmp/p1 p1.cpp
p1.cpp: In function `int main()':
p1.cpp:16: warning: invalid access to non-static data member `Person::m_name' of NULL object
p1.cpp:16: warning: (perhaps the `offsetof' macro was used incorrectly)
p1.cpp:17: warning: invalid access to non-static data member `Child::m_school_year' of NULL object
p1.cpp:17: warning: (perhaps the `offsetof' macro was used incorrectly)
As I read in this newsgroup, the reason is casting 0 to address non-POD structure.
Indeed, the following code produces the same warning:
int main()
{
#define BASE 0x0
printf ( "m_name: %d\n", &((Child*)BASE)->m_school_year );
}
My question is: can I safely ignore this warning for structures of the complexity above?
Again, I found in this newsgroup that solution would be just to turn off this warning
with -Wno-invalid-offsetof flag.
Another solution is, if BASE != 0, then warning is not produced, so that would be the solution.
offsetof macro would look like following:
#define OFFSETOF_BASE 0x1
#define Offsetof(TYPE, MEMBER) \
(size_t)((unsigned char*)(&(((TYPE*)OFFSETOF_BASE)->MEMBER)) - (unsigned char*)(TYPE*)OFFSETOF_BASE)
Regards,
Pawel