In a book called Programming Windows, in one of the examples, we have this line:
ReadFile (hFile, buffer, MAXREAD, &i, NULL) ;
i
here was previously declared as int
, but the 4th argument of ReadFile
is LPDWORD
, which is a typedef for DWORD*
, and DWORD
is a typedef for unsigned long
. It's effectively type punning. On most systems unsigned long
and int
are the same size, but I think accessing a variable as if it were some other type is Undefined Behavior. Is this fine? Is this fine only if the sizes are the same? Is this UB? I checked a couple of errata websites and they don't seem to list this. Am I missing something?
If DWORD
is defined as you describe, then the code contains a constraint violation which the compiler must diagnose , and the Standard no longer covers the behaviour of any executable generated. There is no implicit conversion from int *
to unsigned long *
, regardless of the sizes of types.
If you don't see a compiler error message I would strongly recommend adjusting compiler settings so that an error message is shown. Some compilers default to showing a "warning" message for this mistake which can mislead the unwary into thinking there is no real problem.