【发布时间】:2012-11-08 09:17:02
【问题描述】:
正如MSDN 所说,sizeof(bool) 是 1 字节。但是当我将 bool 放入 struct 时,sizeof struct 变为 4 字节。有人可以解释这种行为吗?
[StructLayout(LayoutKind.Sequential)]
public struct Sample1
{
public bool a1;
}
int size1 = Marshal.SizeOf(typeof (Sample1)); // 4
int size2 = sizeof (bool); // 1
【问题讨论】:
-
Marshal.SizeOf正在测量与sizeof非常不同 的东西... -
这可能与对齐有关。您是否尝试将 Pack=1 添加到 StructLayout 属性的构造函数?
-
@Botz3000,edze,Rawling - 尝试使用 Pack=1,结果相同
标签: c# boolean marshalling