Why in C++ do we use DWORD rather than unsigned int? [duplicate]
Asked Answered
H

4

160

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?

Heterodox answered 8/6, 2010 at 6:42 Comment(2)
It's not typical in c++ per se, it't typical in WinAPI.Bunce
BYTE = 1 letter/char<br>WORD = 2 letter/chars<br>DWORD = 4 letter/chars<br>QWORD = 8 letter/chars. D stand for "double". Q stand for "quad" quad are 2 double(s). D = 32-Bit, DD or Q = 64-Bit. in Assembly: "DB" for DataByte ( 1 times Char). "DW" for DoubleData(WORD - 2 times DB). "DD" for DoubleData(DWORD - 2 times DW). "DQ" for DoubleData(DDWORD - 2 times DD).Broderic
F
209

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.

Fibrilla answered 8/6, 2010 at 6:46 Comment(3)
+1 the explanation. But I still would bet that if they did change DWORD a lot of programs would still break :)Greenman
A DWORD in windows 7 64 bit is unsigned long. Thus it has changed for current developers.Creolacreole
@Milhous: According to the documentation, it is 32 bits: msdn.microsoft.com/en-us/library/cc230318.aspx.Fibrilla
U
113

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.

Uralian answered 8/6, 2010 at 7:39 Comment(4)
What is being referred to by "an unsigned int no longer conforms to the principle of least surprises"?Clarkclarke
I presume he means that the size of int is no longer equal to the maximum native word size of the processor.Drolet
So, does this mean that this is has the same use as stdint.h/cstdint ?Hanuman
For any newbies who read @plugwash's comment: Word-size (generally) tells you how many data bits fit into a single hardware register, which is what determines a ton of low-level things in programming. When you see "word", you expect it to be the same length as the Operating System (e.g., "running on 64-bit Windows" -> 1 word=64 bits long), BUT Windows doesn't conform to this because it wants to remain backwards-compatible so that older systems using the same programs don't break. As a result, we get a 16-bit-long word (i.e., WORD) on a 64-bit computer (instead of a 64-bit-long word).Pluperfect
T
14

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.

Timeless answered 8/6, 2010 at 6:46 Comment(0)
H
13

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.

Heyde answered 8/6, 2010 at 6:47 Comment(3)
8-bit int isn't allowed under the C or C++ standards. But the point remains that there are platforms with 16-bit int and others with 32-bit int.Tabbie
"Word are 16 bits" made sense in 1984 but really doesn't anymore :-(Skitter
"Word are 16 bits so DWORD will be known as 32 bit across all platform" - I'm pretty sure this is incorrect. The term WORD relates more to hardware than software as it is really a term used to describe a single unit of data handled by a particular architecture's instruction set. A WORD could be 8, 16, 32, 64, or any number of bits depending on the architecture.Byzantium

© 2022 - 2024 — McMap. All rights reserved.