首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

请问个有关问题

2012-03-29 
请教个问题#includeiostream.hstruct data{int adouble b}void main(){coutsizeof(data)endl}结

请教个问题
#include<iostream.h>
struct data
{
int a;
double b;
};

void main()
{
cout<<sizeof(data)<<endl;
}
结果为什么是16呢

[解决办法]
看我博客的那篇文章 自然对齐的问题···
[解决办法]
内存对齐了,被和谐了、、、百度“sizeof结构体”
[解决办法]
涉及到 内存对齐的知识 lz 看看博文学习学习吧http://blog.sina.com.cn/s/blog_66e8549c01010qxn.html
加油!青年。。
[解决办法]
内存对齐 以最大的为基准
[解决办法]
这涉及字节对齐问题。这样做的目的是为了能更快地读取数据。具体的请看http://baike.baidu.com/view/1078660.htm
[解决办法]

C/C++ code
#include <iostream>using namespace std;struct data{    int a;    double b;};int main(){    cout<<"the size of int:"<<sizeof(int)<<endl;    cout<<"the size of double:"<<sizeof(double)<<endl;    cout<<"the size of struct data:"<<sizeof(data)<<endl;    return 0;}/*输出结果为:the size of int:4the size of double:8the size of struct data:16原因分析:C/C++里面有一种机制叫做内存字节对齐,用于结构体中,为了提高读写效率才使用该机制,具体你可以百度一下“内存字节对齐”*/ 

热点排行