C++/C#跨语言编程知识点_用栈减小结构体大小和0计算偏移量_class2
#include <iostream>
#define FIELDOFFSET(TYPE, MEMBER) (int)(&(((TYPE*)0)->MEMBER))
//0可以转换为任意类型的结构体或空对象
#pragma pack(push)
#pragma pack(1)
struct Info
{
char username[10];//24-34
double userdata;//40-48
};
#pragma pack(pop)//会变成18
struct Frame
{
unsigned char id;//0-1
int width;//4-8
long long height;//8-16
unsigned char* data;//16-20
Info info;//24-48
};
int main()
{
int len2 = sizeof(Info);//子结构体改变
int len = sizeof(Frame);//母结构体不改变
int offset_w = FIELDOFFSET(Frame, width);
int offset_h = FIELDOFFSET(Frame, height);
int offset_d = FIELDOFFSET(Frame, data);
int offset_i = FIELDOFFSET(Frame, info);
/*len = sizeof(Info);*/
return 0;
}
1.定义0计算偏移量
#define FIELDOFFSET(TYPE, MEMBER) (int)(&(((TYPE*)0)->MEMBER))
int offset_w = FIELDOFFSET(Frame, width);
2.结构体入栈出栈
#pragma pack(push)
#pragma pack(1)
struct Info
{
char username[10];//24-34
double userdata;//40-48..........
};
#pragma pack(pop)//会变成18
原文地址:https://blog.csdn.net/m0_66701835/article/details/142953417
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!