The information in this article applies to:
SUMMARY
When a function call is made in ANSI C, the compiler implicitly casts
the arguments passed to the function to the types specified in the
function's prototype. Implicit casting to promote signed integers or
characters (int, char) to longer unsigned types (DWORD, WORD) can
cause unexpected behavior. The difficulties occur because the signed
shorter value is promoted by extending its sign bit to the high-order
bits of the unsigned longer type.
MORE INFORMATION
In accordance with the ANSI standard, if the shorter value has the
sign bit set, the compiler first converts the value to a signed longer
value by extending the sign. The compiler extends the sign by filling
the high-order bits with 1s. It then converts the signed longer value
to unsigned by adding to it the number that is one larger than the
largest unsigned value of that type. This does not change the bit
pattern in a 2s complement implementation. For more information, see
Section 3.2.1.2 of the ANSI C Standard.
The result of a*b is 38,000 (1001010001110000), and the sign bit of
the int is set. To implement the implicit cast to an unsigned long
value (DWORD), the value is first converted to a signed long value:
The value that is one greater than the largest unsigned long value is
then added, as follows:
GlobalAlloc attempts to allocate 4,294,939,760 bytes of memory rather
than 38,000, and it fails. The GlobalAlloc call in the application
should be as follows:
Problems caused by implicit casting and sign extension are also
encountered frequently when an application passes characters to the
AnsiUpper and AnsiLower functions. The prototypes for these functions
are as follows:
To pass a signed character to AnsiUpper,
Additional query words: 3.00 3.10 no32bit
Keywords : kb16bitonly |
Last Reviewed: November 4, 1999 © 2000 Microsoft Corporation. All rights reserved. Terms of Use. |