I'm trying to understand why in HANDLETABLE struct objectHandle is an array of handles. As I understand, HGDIOBJ is a void pointer and therefore objectHandle is an array of void pointer with an element. But in the book Programming Windows 5th Edition, author said that the field is actually of variable length. How can it work? I think that objectHandle only have 1 element??
Asked
Active
Viewed 57 times
0
-
1The code that allocates the structure allocates enough room for however long the array actually is. But because that length is not known at compile time it is declared as being of length 1. But at run time you can access elements 0 to N-1 inclusive where N is the actual length. – David Heffernan Jul 20 '21 at 10:12
-
1[Why do some structures end with an array of size 1?](https://devblogs.microsoft.com/oldnewthing/20040826-00/?p=38043) – IInspectable Jul 20 '21 at 10:31
-
sorry if any inconvenience but I dont understand how the code that allocates the structure can allocates enough room for however long the array actually is and where does that code implement – titanium Jul 20 '21 at 10:32
-
@duyvu: the only context where I've encountered this particular data type is when enumerating GDI meta files. There, the structure is used to describe what's in the meta file. The number of handles in the structure obviously can't be known ahead of time (when the .h file is written), so this is a way to declare it so that it can accommodate any size encountered at runtime. – 500 - Internal Server Error Jul 20 '21 at 10:50
-
https://en.wikipedia.org/wiki/Flexible_array_member – Hans Passant Jul 20 '21 at 10:56
-
It allocates the memory dynamically with, for example, malloc, or the OS equivalent – David Heffernan Jul 20 '21 at 12:27