结构/类数据对齐和填充的算法?
我似乎找不到任何有关如何在 C、C++ 中的结构或类中计算填充的信息。
在 HLSL 中,存在称为“常量变量”的数据结构,它们非常类似于启用了 #pragma pack (4)
的 C 结构。 此链接了解更多信息关于 HLSL 中的常量变量。
我遇到的问题是尝试创建结构格式描述符。解析 HLSL 代码后,常量描述符将包含有关常量变量结构中包含的数据类型的信息。它将描述每个成员变量的数据类型、其偏移量以及结构体的总大小。我遇到的麻烦是由于填充而确定结构的最终大小。
如果有一个算法,那么我应该能够对其进行编码并计算 HLSL 中任何“常量变量”的实际填充大小。问题是我不知道它是什么,也不知道在哪里可以找到它?
I can't seem to find any information on how to calculate padding within a struct or a class within C, C++.
In HLSL there are data structures called "Constant Variables" and they are very much like C structs with #pragma pack (4)
enabled. Here is a link for more information with regards to Constant Variables in HLSL.
The problem I am having is with trying to create a struct format descriptor. After parsing the HLSL code, a constant descriptor will contain information about the data types contained within a constant variable struct. It will describe the data type of each member variable, its offset and the total size of the struct. The trouble I am having is in determining the final size of a struct due to padding.
If there is an algorithm for this then I should be able to code it up and calculate the actual padded size of any "Constant Variable" in HLSL. The problem is I don't know what it is and nor do I know where to find it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编译器无法在 C 中洗牌数据- 风格结构。 C 保证成员完全按照声明的顺序进行分配。任何填充都必须位于成员之间或结构的末尾。
具有访问说明符的 C++ 类不必与 C 兼容,因此允许进行一些重新排序。
The compiler cannot shuffle data around in a C-style struct. C guarantees that the members are allocated in exactly the order they are declared. Any padding must come between members or at the end of the struct.
A C++ class with access specifiers does not have to be compatible with C, so some reordering is allowed.