我只是试图检查内存如何分配给结构对象,它占用了更多的空间,我期望的。我使用64位Windows操作系统和Microsoft Visual Studio 2010(我认为它的32位),所以可以解释为什么它的打印52字节?为什么结构大小不正确
struct test {
int year;// should take 4 byte
string title;// how much bytes would take ? in my case taking 31 bytes ?
double date;//should take 8 byte
int month;// should take 4 byte
} mine;
int main()
{
cout << " size is: "<<sizeof(mine);//printing 52 ?
cout << " size is: "<<sizeof(struct test);//printing 52 ?
return 0;
}
编译器增加了对路线填充.. – Maroun
那是'string' ='标准:: string'还是什么?如果它是自定义的类/结构体,则应该发布适用类的精简版本(它不需要包含比变量更多的 - 不需要任何函数)。 – Dukeling
看看#pragma pack并阅读有关字节对齐。 – Totonga