Hi All,

I just joined the list and I'm not a compiler guru, so I'd like "the
list" opinion on a behavior I notice today.
Not sure it is really a bug, so do not want to directly open a bug in
the bugzilla repository.

Consider the below sample code:

#include <stdio.h>

int main(int argc, char** argv)
{
        const int i = 0;

        printf("i: %d\n", i);
        foo(&i);
        printf("i: %d\n", i);

        return 0;
}

I compiled the above code without any optimization flag, and with the -O3 flag.

[bash] sborg...@ree> gcc gcc_const_optimazer_bug.c -o gcc_const_optimazer_bug
gcc_const_optimazer_bug.c: In function `main':
gcc_const_optimazer_bug.c:18: warning: passing arg 1 of `foo' discards
qualifiers from pointer target type
[bash] sborg...@ree> ./gcc_const_optimazer_bug
i: 0
i: 42

[bash] sborg...@ree> gcc -O3 gcc_const_optimazer_bug.c -o
gcc_const_optimazer_bug
gcc_const_optimazer_bug.c: In function `main':
gcc_const_optimazer_bug.c:18: warning: passing arg 1 of `foo' discards
qualifiers from pointer target type
[bash] sborg...@ree> ./gcc_const_optimazer_bug
i: 0
i: 0

Now my question is: is it correct that the compiler enforces the
constantness of the variable, even tought it states in the warning
that the const qualifier has been discarded?

Best Regards
Sergio

--
preferisco ammazzare il tempo,
preferisco sparare cazzate,
preferisco fare esplodere una moda,
preferisco morire d'amore.
(Caparezza)

Reply via email to