Why do some structures end with an array of size 1?
来源:互联网 发布:php商城项目经验 编辑:程序博客网 时间:2024/04/28 03:07
from:https://blogs.msdn.microsoft.com/oldnewthing/20040826-00/?p=38043/
Some Windows structures are variable-sized, beginning with a fixed header, followed by a variable-sized array. When these structures are declared, they often declare an array of size 1 where the variable-sized array should be. For example:
typedef struct _TOKEN_GROUPS { DWORD GroupCount; SID_AND_ATTRIBUTES Groups[ANYSIZE_ARRAY];} TOKEN_GROUPS, *PTOKEN_GROUPS;
If you look in the header files, you'll see that ANYSIZE_ARRAY is #define'd to 1, so this declares a structure with a trailing array of size one.
With this declaration, you would allocate memory for one such variable-sized TOKEN_GROUPS structure like this:
PTOKEN_GROUPS TokenGroups = malloc(FIELD_OFFSET(TOKEN_GROUPS, Groups[NumberOfGroups]));
and you would initialize the structure like this:
TokenGroups->GroupCount = NumberOfGroups;for (DWORD Index = 0; Index = NumberOfGroups; Index++) { TokenGroups->Groups[Index] = ...;}
Many people think it should have been declared like this:
typedef struct _TOKEN_GROUPS { DWORD GroupCount;} TOKEN_GROUPS, *PTOKEN_GROUPS;
(In this article, code that is wrong or hypothetical will be italicized.)
The code that does the allocation would then go like this:
PTOKEN_GROUPS TokenGroups = malloc(sizeof(TOKEN_GROUPS) + NumberOfGroups * sizeof(SID_AND_ATTRIBUTES));
This alternative has two disadvantages, one cosmetic and one fatal.
First, the cosmetic disadvantage: It makes it harder to access the variable-sized data. Initializing the TOKEN_GROUPS just allocated would go like this:
TokenGroups->GroupCount = NumberOfGroups;for (DWORD Index = 0; Index = NumberOfGroups; Index++) { ((SID_AND_ATTRIBUTES *)(TokenGroups + 1))[Index] = ...;}
The real disadvantage is fatal. The above code crashes on 64-bit Windows. The SID_AND_ATTRIBUTES structure looks like this:
typedef struct _SID_AND_ATTRIBUTES { PSID Sid; DWORD Attributes; } SID_AND_ATTRIBUTES, * PSID_AND_ATTRIBUTES;
Observe that the first member of this structure is a pointer, PSID. The SID_AND_ATTRIBUTES structure requires pointer alignment, which on 64-bit Windows is 8-byte alignment. On the other hand, the proposed TOKEN_GROUPS structure consists of just a DWORD and therefore requires only 4-byte alignment. sizeof(TOKEN_GROUPS) is four.
I hope you see where this is going.
Under the proposed structure definition, the array of SID_AND_ATTRIBUTES structures will not be placed on an 8-byte boundary but only on a 4-byte boundary. The necessary padding between the GroupCount and the first SID_AND_ATTRIBUTES is missing. The attempt to access the elements of the array will crash with a STATUS_DATATYPE_MISALIGNMENT exception.
Okay, you may say, then why not use a zero-length array instead of a 1-length array?
Because time travel has yet to be perfected.
Zero-length arrays did not become legal Standard C until 1999. Since Windows was around long before then, it could not take advantage of that functionality in the C language.
- Why do some structures end with an array of size 1?
- Model Tree Structures with an Array of Ancestors
- Size of an array
- Why do my text files suddenly have an additional at the end of each line?
- Given an infinite size array with only 0s and 1s and sorted. find the transition point where 0s end
- Why is the size of an empty class not zero?
- Why is the size of an empty class not zero?
- Why is the size of an empty class not zero?
- __curl_rule_01__ declared as an array with a negative size
- '__curl_rule_01__'declared as an array with a negative size
- error C2466: cannot allocate an array of constant size 0
- error C2466: cannot allocate an array of constant size 0
- Find M Integers from an Array of Size N equally
- Why do we need Back-end Testing?
- Why is the size of an empty class not zero in C++?
- Ruby: Sorting an array of strings, in alphabetical order, that includes some arrays of strings
- Java Regex: Start with some characters, end with some characters
- Array of Structures vs Structure of Arrays
- 《SQL注入攻击与防御》
- 设计模式(行为型)--命令模式的应用场景
- PHP中遇到的时区问题解决方法
- 骨骼动画的加载
- 大牛养成指南(2):先实现一个小目标吧!10000小时理论如何轻松落地
- Why do some structures end with an array of size 1?
- 导入带cocopod文件到Cornerstone
- iOS description 方法 引发的所学的大学计算机知识
- Eclipse项目 转为 Studio项目,遇见Error:Error converting bytecode to dex:
- protobuf C++
- 基于空间金字塔池化的卷积神经网络物体检测
- JSON与XML的区别比较以及xml两种解析方式的比较
- 什么是构造器
- Win7系统下 Java环境变量配置