为什么在 C++我们使用 DWORD 而不是 unsigned int 吗?

发布于 2024-09-04 21:00:56 字数 197 浏览 6 评论 0原文

我并不害怕承认我是一个 C++ 新手,所以这可能看起来是一个愚蠢的问题但是......

我看到代码示例中到处都使用了 DWORD。当我查找 DWORD 的真正含义时,它显然只是一个无符号整数(0 到 4,294,967,295)。那么我的问题是,为什么我们有 DWORD?它给了我们什么是整型“unsigned int”没有给我们的?这与便携性和机器差异有关系吗?

I'm not afraid to admit that I'm somewhat of a C++ newbie, so this might seem like a silly question but....

I see DWORD used all over the place in code examples. When I look up what a DWORD truly means, its apparently just an unsigned int (0 to 4,294,967,295). So my question then is, why do we have DWORD? What does it give us that the integral type 'unsigned int' does not? Does it have something to do with portability and machine differences?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浊酒尽余欢 2024-09-11 21:00:56

DWORD 不是 C++ 类型,它在 中定义。

原因是 DWORD 具有 Windows 函数所依赖的特定范围和格式,因此如果您需要该特定范围,请使用该类型。 (或者正如他们所说的“入乡随俗。”)对于您来说,这恰好对应于 unsigned int,但情况可能并不总是如此。为了安全起见,当需要 DWORD 时,请使用 DWORD,无论它实际上是什么。

例如,如果他们更改了 unsigned int 的范围或格式,他们可以使用不同的类型来底层 DWORD 来保持相同的要求,并且所有代码都使用 DWORD 就更不明智了。 (同样,他们可能会决定 DWORD 需要为 unsigned long long,更改它,并且所有使用 DWORD 的代码都将变得不明智.)


还要注意unsigned int不一定的范围是0到4,294,967,295。请参阅此处。

DWORD is not a C++ type, it's defined in <windows.h>.

The reason is that DWORD has a specific range and format Windows functions rely on, so if you require that specific range use that type. (Or as they say "When in Rome, do as the Romans do.") For you, that happens to correspond to unsigned int, but that might not always be the case. To be safe, use DWORD when a DWORD is expected, regardless of what it may actually be.

For example, if they ever changed the range or format of unsigned int they could use a different type to underly DWORD to keep the same requirements, and all code using DWORD would be none-the-wiser. (Likewise, they could decide DWORD needs to be unsigned long long, change it, and all code using DWORD would be none-the-wiser.)


Also note unsigned int does not necessary have the range 0 to 4,294,967,295. See here.

巨坚强 2024-09-11 21:00:56

当MS-DOS和Windows 3.1以16位模式运行时,Intel 8086字是16位,Microsoft WORD是16位,Microsoft DWORD是32位,典型编译器的unsigned int是16位。

当Windows NT以32位模式运行时,Intel 80386字为32位,Microsoft WORD为16位,Microsoft DWORD为32位,典型编译器的unsigned int为32位。名称 WORD 和 DWORD 不再具有自我描述性,但它们保留了 Microsoft 程序的功能。

当Windows运行在64位模式时,一个Intel字是64位,一个Microsoft WORD是16位,一个Microsoft DWORD是32位,典型编译器的unsigned int是32位。名称 WORD 和 DWORD 不再具有自我描述性,并且 unsigned int 不再符合最少意外原则,但它们保留了许多程序的功能。

我认为 WORD 或 DWORD 永远不会改变。

When MS-DOS and Windows 3.1 operated in 16-bit mode, an Intel 8086 word was 16 bits, a Microsoft WORD was 16 bits, a Microsoft DWORD was 32 bits, and a typical compiler's unsigned int was 16 bits.

When Windows NT operated in 32-bit mode, an Intel 80386 word was 32 bits, a Microsoft WORD was 16 bits, a Microsoft DWORD was 32 bits, and a typical compiler's unsigned int was 32 bits. The names WORD and DWORD were no longer self-descriptive but they preserved the functionality of Microsoft programs.

When Windows operates in 64-bit mode, an Intel word is 64 bits, a Microsoft WORD is 16 bits, a Microsoft DWORD is 32 bits, and a typical compiler's unsigned int is 32 bits. The names WORD and DWORD are no longer self-descriptive, AND an unsigned int no longer conforms to the principle of least surprises, but they preserve the functionality of lots of programs.

I don't think WORD or DWORD will ever change.

木格 2024-09-11 21:00:56

SDK 开发人员更喜欢使用 typedef 定义自己的类型。这允许仅在一处更改底层类型,而无需更改所有客户端代码。遵守这一惯例很重要。 DWORD 不太可能更改,但 DWORD_PTR 等类型在不同平台(如 Win32 和 x64)上有所不同。因此,如果某个函数具有 DWORD 参数,请使用 DWORD 而不是 unsigned int,并且您的代码将在所有未来的 Windows 标头版本中进行编译。

SDK developers prefer to define their own types using typedef. This allows changing underlying types only in one place, without changing all client code. It is important to follow this convention. DWORD is unlikely to be changed, but types like DWORD_PTR are different on different platforms, like Win32 and x64. So, if some function has DWORD parameter, use DWORD and not unsigned int, and your code will be compiled in all future windows headers versions.

多彩岁月 2024-09-11 21:00:56

就我自己而言,我假设 unsigned int 是特定于平台的。整数可以是 16 位、32 位甚至 64 位。

另一方面,DWORD 指定它自己的大小,即双字。字为 16 位,因此在所有平台上 DWORD 将被称为 32 位。

For myself, I would assume unsigned int is platform specific. Integers could be 16 bits, 32 bits or even 64 bits.

On the other hand, DWORD specifies its own size, which is Double Word. Words are 16 bits, so DWORD will be known as 32 bits across all platforms.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文