#include <stdlib.h>
#include <stddef.h>
#include <stdio.h>
struct Entry
{
int key;
int value;
struct Entry *next;
};
typedef struct Entry Entry_t;
class BEntry
{
///*
public:
int common;
//*/
};
class CEntry
///*
: public BEntry
//*/
{
public:
/*
static
*/
int key;
/*
static
*/
int value;
/*
static
*/
CEntry *next;
///*
CEntry() {
}
//*/
};
int main(int argc, char** argv)
{
// c-style part
printf("offset of field key of Entry_t: %ld\n", offsetof(Entry_t, key));
printf("offset of field value of Entry_t: %ld\n", offsetof(Entry_t, value));
printf("offset of field next of Entry_t: %ld\n", offsetof(Entry_t, next));
Entry_t entry;
entry.key = 1;
entry.value = 11;
entry.next = NULL;
//*
// c++-style part
//printf("offset of field common of CEntry: %ld\n", offsetof(CEntry, common));
printf("offset of field key of CEntry: %ld\n", offsetof(CEntry, key));
printf("offset of field value of CEntry: %ld\n", offsetof(CEntry, value));
printf("offset of field next of CEntry: %ld\n", offsetof(CEntry, next));
//*/
}
在编译的时候出现告警信息,但后面链接运行时正常, 且输出结果正确。
D:\home\admin\workstation\cpp\cpptest>g++ -c offsetoftest.cpp -o offsetoftest.o
In file included from /usr/include/sys/cdefs.h:45:0,
from /usr/include/stdio.h:35,
from offsetoftest.cpp:4:
offsetoftest.cpp: In function ‘int main(int, char**)’:
offsetoftest.cpp:63:66: warning: invalid access to non-static data member ‘CEntry::key’ of NULL object [-Winvalid-offsetof]
printf("offset of field key of CEntry: %ld\n", offsetof(CEntry, key));
^
offsetoftest.cpp:63:66: warning: (perhaps the ‘offsetof’ macro was used incorrectly) [-Winvalid-offsetof]
offsetoftest.cpp:64:68: warning: invalid access to non-static data member ‘CEntry::value’ of NULL object [-Winvalid-offsetof]
printf("offset of field value of CEntry: %ld\n", offsetof(CEntry, value));
^
offsetoftest.cpp:64:68: warning: (perhaps the ‘offsetof’ macro was used incorrectly) [-Winvalid-offsetof]
offsetoftest.cpp:65:67: warning: invalid access to non-static data member ‘CEntry::next’ of NULL object [-Winvalid-offsetof]
printf("offset of field next of CEntry: %ld\n", offsetof(CEntry, next));
^
offsetoftest.cpp:65:67: warning: (perhaps the ‘offsetof’ macro was used incorrectly) [-Winvalid-offsetof]
D:\home\admin\workstation\cpp\cpptest>g++ offsetoftest.o -o offsetoftest
D:\home\admin\workstation\cpp\cpptest>offsetoftest
offset of field key of Entry_t: 0
offset of field value of Entry_t: 4
offset of field next of Entry_t: 8
offset of field key of CEntry: 4
offset of field value of CEntry: 8
offset of field next of CEntry: 12
但如果CEntry不继承BEntry的话, 去掉:
///*
: public BEntry
//*/
这块的注释, 在编译时就不会出现告警信息,后面链接运行也正常, 输出结果正确。
D:\home\admin\workstation\cpp\cpptest>g++ -c offsetoftest.cpp -o offsetoftest.o
D:\home\admin\workstation\cpp\cpptest>g++ offsetoftest.o -o offsetoftest
D:\home\admin\workstation\cpp\cpptest>offsetoftest.exe
offset of field key of Entry_t: 0
offset of field value of Entry_t: 4
offset of field next of Entry_t: 8
offset of field key of CEntry: 0
offset of field value of CEntry: 4
offset of field next of CEntry: 8