可能是一个愚蠢的问题......我是C#和.Net的新手.
在MSDN上的SafeHandle类(C#)的示例中,代码让我抓狂了一下.
[SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
internal class MySafeFileHandle : SafeHandleZeroOrMinusOneIsInvalid
{
private MySafeFileHandle()
: base(true)
{}
// other code here
}
[SuppressUnmanagedCodeSecurity()]
internal static class NativeMethods
{
// other code...
// Allocate a file object in the kernel, then return a handle to it.
[DllImport("kernel32", SetLastError = true, CharSet = CharSet.Unicode)]
internal extern static MySafeFileHandle CreateFile(String fileName,
int dwDesiredAccess, System.IO.FileShare dwShareMode,
IntPtr securityAttrs_MustBeZero, System.IO.FileMode
dwCreationDisposition, int dwFlagsAndAttributes,
IntPtr hTemplateFile_MustBeZero);
// other code...
}
// …Run Code Online (Sandbox Code Playgroud) 我在某处读到使用BOOL(typedef int)比使用标准c ++类型bool更好,因为BOOL的大小是4个字节(即4的倍数),它将变量的对齐操作保存到寄存器或沿着这些行的某些东西. .
有没有道理呢?我想编译器会填充堆栈帧,以便即使你使用bool(1字节)也能保持多个4的对齐?
我绝不是对齐,寄存器等基础工作的专家,所以如果我完全错了,我会提前道歉.我希望得到纠正.:)
干杯!