具有char,double,int和t的结构的大小 [英] Size of structure with a char, a double, an int and a t
本文介绍了具有char,double,int和t的结构的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我只运行代码片段
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;
它给我这样的结果:
1
8
4
4
b $ b
总计:17。
但是当我测试包含这些数据类型的sizeof结构时,它给我24,我很困惑。
$ b < #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 :: end1;
printf(s_type is%d byes long,sizeof(struct struct_type));
return 0;
}
:EDIT
我已经更新了这样的代码
#include< iostream>
#include< stdio.h>
struct struct_type {
double d_attribute;
int i__attribute __(int(packed));
int * p__attribute_(int(packed));;
char ch;
} 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 bytes long,sizeof(s));
return 0;
}
现在它显示我16个字节。