在对齐之前获取实际结构大小
因此,事实证明, dt struct
和? sizeof struct
返回对齐后结构在内存中占据的总尺寸。
有没有办法在对齐之前获取结构的实际尺寸?
我需要此功能,用于返回结构内字段的实际大小的函数。例如:
__declspec(align(64)) struct ALIGNED_STRUCT {
char field;
}
运行 ?? sizeof(aligned_struct)
应该获得0x40,这使得很难推断出内部字段的实际大小。
编辑:
命令输出:
2:001> dt -v ALIGNED_STRUCT
test!ALIGNED_STRUCT
struct ALIGNED_STRUCT, 1 elements, 0x40 bytes
+0x000 field : Char
3:001> ?? sizeof(ALIGNED_STRUCT)
0x40
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不 - 没有办法返回“对齐前”结构大小。无论如何,这并不是真正有意义的。编译器始终使用对齐尺寸。符号具有对齐的大小。那就是类型的大小。
如果您正在寻找诸如“内部字段的大小”之类的东西,那么有很多方法可以实现这一目标。如评论中所述,您可以执行快速的肮脏ee sizeof操作:
您也可以通过数据模型API(在A C/C ++扩展名或JavaScript中)获得完整访问对基础类型的系统进行)这将使您几乎可以找到有关类型的任何内容:它们的字段,尺寸,偏移,功能参数类型,模板参数等...
从C/C ++,您可以:
通过调用可能看起来像 之类的
:对于C ++,可以通过使用该<> 来更轻松地使其更容易。 C ++ 17助手库(
.microsoft.com/en-us/windows-hardware/drivers/debugger/natiber-objects-in-javascript-extensions-extensions-type-objects“ rel =” nofollow noreferrer“> https://learn.microsoft.com/microsoft.com/en--en--- US/Windows-Hardware/drivers/debugger/native-objects-in-javascript-extensions-type-objects ),这看起来像:
,通过访问 可能看起来 :
在任何一种情况下,如果您要查看基本类中包含字段的C ++对象,则需要手动重复基础类。
希望有帮助...
No -- there isn't a way to return the structure size "before alignment". That's not really meaningful in any case. The compiler is always using the aligned size. The symbols have the aligned size. That's the size of the type.
If you are looking for things like the "size of an internal field", there are numerous ways to accomplish this. As mentioned in comments, you can do the quick dirty EE sizeof thing:
You can also get full access to the underlying type system via the data model APIs (in either a C/C++ extension or in JavaScript) which will allow you to find out pretty much whatever you want about the types: their fields, sizes, offsets, function parameter types, template arguments, etc...
From C/C++, you can:
That might look something like this:
For C++, this can be made significantly easier by using the C++17 helper library on GitHub (https://github.com/microsoft/WinDbg-Libraries/blob/master/DbgModelCppLib/DbgModelClientEx.h)
That might look something like:
In JavaScript (see https://learn.microsoft.com/en-us/windows-hardware/drivers/debugger/native-objects-in-javascript-extensions-type-objects), this would look like:
That might look something like:
In either of these cases, you would need to manually recurse base classes if you're looking at C++ objects where fields are contained in base classes.
Hope that helps...
威廉提供了一个全面的答案,这个答案只是一个实用的例子
William has provided a comprehensive answer this answer is just a practical example of that