windowsguidsizeof

Can I assume sizeof(GUID)==16 at all times?


The definition of GUID in the windows header's is like this:

typedef struct _GUID {
unsigned long Data1;
unsigned short Data2;
unsigned short Data3;
unsigned char Data4[ 8 ];
} GUID;

However, no packing is not defined. Since the alignment of structure members is dependent on the compiler implementation one could think this structure could be longer than 16 bytes in size.

If i can assume it is always 16 bytes - my code using GUIDs is more efficient and simple. However, it would be completely unsafe - if a compiler adds some padding in between of the members for some reason.

My questions do potential reasons exist ? Or is the probability of the scenario that sizeof(GUID)!=16 actually really 0.


Solution

  • It's not official documentation, but perhaps this article can ease some of your fears. I think there was another one on a similar topic, but I cannot find it now.

    What I want to say is that Windows structures do have a packing specifier, but it's a global setting which is somewhere inside the header files. It's a #pragma or something. And it is mandatory, because otherwise programs compiled by different compilers couldn't interact with each other - or even with Windows itself.