开发者

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

开发者 https://www.devze.com 2023-03-25 04:52 出处:网络
The definition of GUID in the windows header\'s is like this: typedef struct _GUID { unsigned long Data1;

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 alway开发者_JAVA技巧s 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.


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.


It's not zero, it depends on your system. If the alignment is word (4-bytes) based, you'll have padding between the shorts, and the size will be more than 16.

If you want to be sure that it's 16 - manually disable the padding, otherwise use sizeof, and don't assume the value.


If I feel I need to make an assumption like this, I'll put a 'compile time assertion' in the code. That way, the compiler will let me know if and when I'm wrong.

If you have or are willing to use Boost, there's a BOOST_STATIC_ASSERT macro that does this.

For my own purposes, I've cobbled together my own (that works in C or C++ with MSVC, GCC and an embedded compiler or two) that uses techniques similar to those described in this article:

  • http://www.pixelbeat.org/programming/gcc/static_assert.html

The real tricks to getting the compile time assertion to work cleanly is dealing with the fact that some compilers don't like declarations mixed with code (MSVC in C mode), and that the techniques often generate warnings that you'd rather not have clogging up an otherwise working build. Coming up with techniques that avoid the warnings is sometimes a challenge.


Yes, on any Windows compiler. Otherwise IsEqualGUID would not work: it compares only the first 16 bytes. Similarly, any other WinAPI function that takes a GUID* just checks the first 16 bytes.

Note that you must not assume generic C or C++ rules for windows.h. For instance, a byte is always 8 bits on Windows, even though ISO C allows 9 bits.


Anytime you write code dependent on the size of someone else's structure, warning bells should go off.

Could you give an example of some of the simplified code you want to use? Most people would just use sizeof(GUID) if the size of the structure was needed.

With that said -- I can't see the size of GUID ever changing.


#include <stdio.h>
#include <rpc.h>
int main () {
GUID myGUID;
printf("size of GUID is %d\n", sizeof(myGUID));
return 0;
}

Got 16. This is useful to know if you need to manually allocate on the heap.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号