PRB: Compiler Generates C4056, C4756 Warnings IncorrectlyLast reviewed: July 17, 1997Article ID: Q97839 |
6.00 6.00a 6.00ax 7.00 | 1.00 1.50 1.51 1.52
MS-DOS | WINDOWSkbtool kbprb
The information in this article applies to:
SYMPTOMSWhen it compiles the code sample below, Microsoft C generates one of the following warning messages. In Microsoft C/C++ versions 7.0 and later:
warning C4756: overflow in constant arithmeticIn Microsoft C versions 6.0, 6.0a, and 6.0ax:
warning C4056: overflow in constant arithmeticThe compiler generates the correct code; the warning is erroneous.
CAUSEThe optimizing compiler performs strength reduction for equations by removing parentheses from the equation when the resulting equation is mathematically equivalent. The warning occurs when strength reduction creates a constant that is too large for its data type. The compiler can create such a constant because the ANSI standard does not mandate that the compiler honor parentheses as long as the resulting equation is mathematically equivalent. After the compiler performs strength reduction, the compiler cannot determine where the parentheses were and does not provide any method to suppress the warning message.
RESOLUTIONThere are two methods to work around this warning message:
MORE INFORMATIONThe following sample code demonstrates this warning message. When the compiler performs strength reduction on the code below, it creates the following equation:
result += number * 31536000L - 63072000000L;The warning does not occur if you replace the equation with the following:
long temp = 0L; temp = number - 2000L; result += temp * 31536000L; Sample Code
/* * Compile options needed: /f- /W2 */long result = 0L; long number = 2010L;
void main (void){ result += (number - 2000L) * 31536000L;}
|
Additional reference words: 6.00 6.00a 6.00ax 7.00 8.00 8.00c
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |