【发布时间】:2013-07-20 09:40:15
【问题描述】:
我在尝试使用命名空间和结构时遇到了问题。
C++
#include<iostream>
using namespace std;
namespace One
{
struct Data
{
int val;
char character;
};
}
namespace Two
{
struct Data
{
int val;
bool boolean;
};
}
void functionOne(void)
{
using namespace One;
cout << "functionOne()" << endl;
cout << "The size of struct Data : ";
cout << sizeof(Data) << endl;
}
void functionTwo(void)
{
using namespace Two;
cout << "functionTwo()" << endl;
cout << "The size of struct Data : ";
cout << sizeof(Data) << endl;
}
int main()
{
functionOne();
functionTwo();
}
Output
functionOne()
The size of struct Data : 8
functionTwo()
The size of struct Data : 8
当我将“命名空间二”的代码更改为以下内容时:
namespace Two
{
struct Data
{
char val;
bool boolean;
};
}
Output :
functionOne()
The size of struct Data : 8
functionTwo()
The size of struct Data : 2
我无法弄清楚编译器如何为结构分配内存。提前致谢。
【问题讨论】:
-
@CarlNorum 链接中的答案说明了 32 位架构。我可以知道对齐(通常)是在 64 位架构上完成的吗?
-
@mozart,你的例子可能是一样的。
标签: c++ namespaces