Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 710
JE42 Avatar asked Aug 04 '11 22:08

JE42


1 Answers

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.

like image 127
Vilx- Avatar answered Sep 29 '22 18:09

Vilx-