7

This is a follow up question to this.

Can it be that DWORD is same as int on some platform(How to check this?)? If that is the case, then this:

  DWORD v1, v2, v3, Build;
  GetVersion(&v1, &v2, &v3, &Build);
  sprintf(VersionStr, "%d.%d.%d.%d", v1, v2, v3, Build);

is not undefined behaviour on that platform, am I right??

Or above code is ALWAYS and everywhere UB?

How to check whether DWORD is same as int on that platform?

(because maybe I am thinking on that computer where this code is run above is not undefined behaviour)

Community
  • 1
  • 1
  • I have found this piece of code in my Visual Studio 2013 environment in Windows 7: `typedef unsigned long DWORD;` – Dominique Nov 12 '15 at 12:50
  • https://msdn.microsoft.com/en-us/library/windows/desktop/aa383751%28v=vs.85%29.aspx – Some programmer dude Nov 12 '15 at 12:50
  • @JoachimPileborg: So my code is ALWAYS UB? –  Nov 12 '15 at 12:51
  • @BoPersson: So my code is ALWAYS UB? –  Nov 12 '15 at 12:55
  • "How to check this?" Make sure the relevant compiler warnings are all on. Clang, for example, will warn if the arguments to a `printf` don't match the ones in the format string. – Jongware Nov 12 '15 at 12:55
  • @Jongware: I meant how to double check DWORD is not int –  Nov 12 '15 at 12:56
  • 4
    It's never *just* `int`. It could be `unsigned long`, it could be `unsigned int`, the important thing here is that it's *always* `unsigned` and always 32-bits. – Some programmer dude Nov 12 '15 at 12:57
  • And I suggest having your compiler check it. – Jongware Nov 12 '15 at 12:57
  • 2
    Yes it's undefined behavior to use the wrong format, even for such simple things as `signed` versus `unsigned`. See e.g. http://stackoverflow.com/q/5851524/440558 – Some programmer dude Nov 12 '15 at 13:00
  • @JoachimPileborg: Even for those unsigned values which can fit in int? –  Nov 12 '15 at 13:04
  • Btw, I think it is coming close to an MSVC implementation defect that these ubiquitously used type definitions do not come with corresponding format definitions/macros. (Or did I miss them when I browsed the MSDN?) My guess is that, apart from originally targeting 16 bit Intel CPUs and not thinking ahead much, everybody was also less paranoid 20 years ago than today -- partly because there were fewer reasons to be paranoid because the compilers were simpler. – Peter - Reinstate Monica Nov 12 '15 at 13:18
  • @BoPersson “`DWORD` is … always unsigned” — Always? `using DWORD = signed char*[2]; // double word`. – Konrad Rudolph Nov 12 '15 at 13:18

1 Answers1

5

On a 32 bit compiler, the format specifier which you can use to print the value of DWORD is %lu. You can also use %ld if you want to print the value in decimal format.

You can also refer a thread: Why in C++ do we use DWORD rather than unsigned int?

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331