windows 我可以一直假设 sizeof(GUID)==16 吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6949598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:42:38  来源:igfitidea点击:

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

windowsguidsizeof

提问by JE42

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

windows header 中 GUID 的定义是这样的:

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.

但是,没有定义没有包装。由于结构成员的对齐取决于编译器的实现,因此人们可能会认为该结构的大小可能超过 16 个字节。

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.

如果我可以假设它总是 16 个字节 - 我使用 GUID 的代码更高效和简单。但是,这将是完全不安全的 - 如果编译器出于某种原因在成员之间添加了一些填充。

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

我的问题是否存在潜在原因?或者是 sizeof(GUID)!=16 这个场景的概率实际上真的是 0。

采纳答案by Vilx-

It's not official documentation, but perhaps this articlecan 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 #pragmaor something. And it is mandatory, because otherwise programs compiled by different compilers couldn't interact with each other - or even with Windows itself.

我想说的是 Windows 结构确实有一个打包说明符,但它是一个全局设置,位于头文件中的某处。这是一个#pragma什么的。这是强制性的,因为否则由不同编译器编译的程序无法相互交互 - 甚至无法与 Windows 本身交互。

回答by littleadv

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.

它不是零,这取决于您的系统。如果对齐是基于字(4 字节),则shorts之间会有填充,并且大小将超过 16。

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

如果您想确定它是 16 - 手动禁用填充,否则使用sizeof,并且不要假设该值。

回答by Michael Burr

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_ASSERTmacro that does this.

如果你有或愿意使用 Boost,有一个BOOST_STATIC_ASSERT宏可以做到这一点。

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:

出于我自己的目的,我拼凑了我自己的(使用 MSVC、GCC 和一两个嵌入式编译器在 C 或 C++ 中工作),使用的技术与本文中描述的技术类似:

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.

让编译时断言干净利落地工作的真正技巧是处理这样一个事实,即某些编译器不喜欢与代码混合的声明(C 模式下的 MSVC),并且这些技术经常生成警告,您宁愿不要阻塞建立一个其他有效的构建。提出避免警告的技术有时是一个挑战。

回答by MSalters

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

是的,在任何 Windows 编译器上。否则IsEqualGUID将不起作用:它只比较前 16 个字节。类似地,任何其他接受 a 的 WinAPI 函数GUID*只检查前 16 个字节。

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.

请注意,您不得为windows.h. 例如,一个字节在 Windows 上总是 8 位,即使 ISO C 允许 9 位。

回答by MessyHack

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.

你能举一个你想使用的一些简化代码的例子吗?如果需要结构的大小,大多数人只会使用 sizeof(GUID)。

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

话虽如此 - 我看不到 GUID 的大小不断变化。

回答by MessyHack

#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.

得到 16。这有助于了解是否需要在堆上手动分配。