This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: how to make gcc warn about arithmetic signed overflow
- From: wempwer at gmail dot com
- To: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- Cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Sat, 21 Sep 2013 20:50:53 +0200
- Subject: Re: how to make gcc warn about arithmetic signed overflow
- Authentication-results: sourceware.org; auth=none
- References: <20130921164609 dot GC3086 at a dot lan> <CAH6eHdTToM+TMy55m5HYo39DC8nA0RrTma1Bp5OnhUtPErMfOA at mail dot gmail dot com> <20130921174229 dot GD3086 at a dot lan> <CAH6eHdQzJNQY4Meysi259RheSaGscKUF28OW43OvOD1rf6FkxQ at mail dot gmail dot com>
On Sat, Sep 21, 2013 at 07:30:02PM +0100, Jonathan Wakely wrote:
> On 21 September 2013 18:42, <wempwer@gmail.com> wrote:
> >
> > 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,
>
> So the compiler knows that the values of ab and bc, because they must
> always have the values they were initialized with.
>
> If the compiler was smarter it would be able to also warn for
> non-const values if it can prove that nothing has modified the
> variables since they were initialized.
>
> > its value can be changed using pointers
>
> No, that's not true. You can't change the value of a const object in a
> valid program.
>
> > but the result is undefined and that const != constant expression.
I think I can but it may cause an undefined behavior:
const int bc = 50000;
int *p = &bc;
*p = 10;
printf("Modified const: %d\n", bc);
10 will be printed.
--
<wempwer@gmail.com>