我在一段代码中有以下结构(在 x86 上编译):
0:012> dt prog!_TESTSTRUCT
+0x000 __VFN_table : Ptr32
+0x008 szAddr : Ptr32 Wchar
+0x00c szOpCode : Ptr32 Wchar
+0x010 szMnemonic : Ptr32 Wchar
+0x014 szArgs : Ptr32 Wchar
+0x018 addr : Uint8B
+0x020 nextOffset : Uint8B
如您所见,vtable ptr 虽然大小为 4 个字节,但与 8 个字节对齐。
作为引用,这里是同一段代码中的另一个结构,通常是对齐的:
0:012> dt prog!_TESTSTRUCT2
+0x000 __VFN_table : Ptr32
+0x004 pClient : Ptr32 IDebugClient5
+0x008 pDebugControl : Ptr32 IDebugControl4
+0x00c pDebugSystemObjects : Ptr32 IDebugSystemObjects
+0x010 pDebugDataSpaces : Ptr32 IDebugDataSpaces
+0x014 pDebugSymbols : Ptr32 IDebugSymbols3
+0x018 handlesAcquired : Bool
为什么 x86 上的 32 位 ptr 与第一个结构中的 8 个字节对齐?我在这里缺少什么?
编辑 1:
class _TESTSTRUCT size(40):
1> +---
1> 0 | {vfptr}
1> 8 | szAddr
1> 12 | szOpCode
1> 16 | szMnemonic
1> 20 | szArgs
1> 24 | addr
1> 32 | nextOffset
1> +---
1>
1> _TESTSTRUCT::$vftable@:
1> | &_TESTSTRUCT_meta
1> | 0
1> 0 | &_TESTSTRUCT::{dtor}
1>
1> _TESTSTRUCT::{dtor} this adjustor: 0
1> _TESTSTRUCT::__delDtor this adjustor: 0
1> _TESTSTRUCT::__vecDelDtor this adjustor: 0
class _TESTSTRUCT2 size(28):
1> +---
1> 0 | {vfptr}
1> 4 | pDebugClient
1> 8 | pDebugControl
1> 12 | pDebugSystemObjects
1> 16 | pDebugDataSpaces
1> 20 | pDebugSymbols
1> 24 | handlesAcquired
1> | <alignment member> (size=3)
1> +---
1>
1> _TESTSTRUCT2::$vftable@:
1> | &_TESTSTRUCT2_meta
1> | 0
1> 0 | &_TESTSTRUCT2::{dtor}
1>
1> _TESTSTRUCT2::{dtor} this adjustor: 0
1> _TESTSTRUCT2::__delDtor this adjustor: 0
1> _TESTSTRUCT2::__vecDelDtor this adjustor: 0
最佳答案
正如这里所解释的
http://lolengine.net/blog/2012/10/21/the-stolen-bytes
8 字节结构成员导致 vfptr 未对齐到 8 字节而不是 4 字节,因为 vfptr 在所有其他成员添加/对齐后添加到结构中。此行为似乎是 Visual Studio 特定的
关于visual-studio-2012 - 为什么 vtable 指针在 x86 编译程序上对齐到 8 个字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27764464/