C/C++ offsetof编译问题

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 C 或 C++ 中,计算一个对象所占用的空间大小相对比较简单。 对于 C 语言,我们可以使用 `sizeof` 运算符来计算一个对象的大小,例如: ```c #include <stdio.h> #include <stdlib.h> int main() { int a; printf("Size of int: %ld bytes\n", sizeof(a)); return 0; } ``` 上述代码将输出 `Size of int: 4 bytes`,表示 `int` 类型所占用的空间大小为 4 个字节。 对于 C++ 语言,我们同样可以使用 `sizeof` 运算符来计算一个对象的大小,例如: ```c++ #include <iostream> using namespace std; int main() { int a; cout << "Size of int: " << sizeof(a) << " bytes" << endl; return 0; } ``` 上述代码将输出 `Size of int: 4 bytes`,表示 `int` 类型所占用的空间大小为 4 个字节。 需要注意的是,对于结构体和类等复杂类型的对象,其大小取决于其成员变量的类型和数量,需要逐个计算并累加,可以使用 `sizeof` 运算符和 `offsetof` 宏来计算。例如: ```c++ #include <iostream> #include <cstddef> using namespace std; struct Person { string name; int age; }; int main() { Person p; cout << "Size of Person: " << sizeof(p) << " bytes" << endl; cout << "Offset of name: " << offsetof(Person, name) << " bytes" << endl; cout << "Offset of age: " << offsetof(Person, age) << " bytes" << endl; return 0; } ``` 上述代码将输出 `Size of Person: 16 bytes`,表示 `Person` 类型所占用的空间大小为 16 个字节,其中 `name` 字符串占用 8 个字节,`age` 整数占用 4 个字节,剩余 4 个字节为内存对齐所需的填充字节。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值