C/C++ offsetof编译问题

本文讨论了在C++中使用offsetof宏时遇到的编译警告,特别是在涉及非静态成员和继承的情况下。尽管编译时出现警告,但程序在链接并运行后仍能正确输出字段偏移量。当类CEntry继承自BEntry时,编译警告消失,表明offsetof使用正确。
摘要由CSDN通过智能技术生成

#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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值