对于某些编译器,有一个结构的打包说明符,例如::
RealView ARM compiler has "__packed" Gnu C Compiler has "__attribute__ ((__packed__))" Visual C++ has no equivalent, it only has the "#pragma pack(1)"
我需要一些我可以放入结构定义的东西.
任何信息/黑客/建议?TIA ...
当我只运行代码片段时
int *t;
std::cout << sizeof(char) << std::endl;
std::cout << sizeof(double) << std::endl;
std::cout << sizeof(int) << std::endl;
std::cout << sizeof(t) << std::endl;
Run Code Online (Sandbox Code Playgroud)
它给我一个这样的结果:
1
8
4
4
Run Code Online (Sandbox Code Playgroud)
总计:17.
但是,当我测试包含这些数据类型的sizeof结构时,它给了我24,我很困惑.额外的7个字节是多少?
这是代码
#include <iostream>
#include <stdio.h>
struct struct_type{
int i;
char ch;
int *p;
double d;
} s;
int main(){
int *t;
//std::cout << sizeof(char) <<std::endl;
//std::cout << sizeof(double) <<std::endl;
//std::cout << sizeof(int) <<std::endl;
//std::cout << sizeof(t) <<std::endl;
printf("s_type is %d byes long",sizeof(struct struct_type));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
:编辑
我已经像这样更新了我的代码
#include <iostream> …Run Code Online (Sandbox Code Playgroud)