how to make gcc warn about arithmetic signed overflow

wempwer@gmail.com wempwer@gmail.com
Sat Sep 21 17:41:00 GMT 2013


On Sat, Sep 21, 2013 at 06:24:13PM +0100, Jonathan Wakely wrote:
> On 21 September 2013 17:46,  wrote:
> > Is it possible for gcc to produce a
> > warning in such situation?
> 
> If ab and bc are 'const' then G++ will warn:
> 
> o.cc: In function ‘int main()’:
> o.cc:5:14: warning: integer overflow in expression [-Woverflow]
>  int r = ab * bc;
>               ^
> 
> The C compiler still doesn't warn though, due to different rules for
> how constants are handled between C and C++.

Hmm, I don't know too much about C++ but why would a C++ compiler
produce a warning only if two operands were const? I learned that in C
`const' modifier only means that I promise to the compiler that this
variable will be read-only, its value can be changed using pointers
but the result is undefined and that const != constant expression. So
are the differences in const between C and C++ so much complex or is
it just a compiler that decides to act this way? From what you said, I
believe the differnces are more complex than I thought.

Anyway, do you know how to make gcc warn about this in pure C?
-- 
<wempwer@gmail.com>



More information about the Gcc-help mailing list