This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/15437] int vs const int computation: different answers
- From: "bangerth at dealii dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 14 May 2004 22:13:22 -0000
- Subject: [Bug c/15437] int vs const int computation: different answers
- References: <20040514162247.15437.lani@oas.ca>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Additional Comments From bangerth at dealii dot org 2004-05-14 22:13 -------
I see your point, but I don't think you are entirely right. If you
store something in a 'const int' you tell the compiler that the value
of the variable won't change. The compiler can then do some simple
transformations based on this knowledge, without having to resort
to doing these things at run-time. Note that within the margin of
errors compiler-internal transformations and run-time behavior might
differ.
If you just store it in an 'int', the compiler has to do some serious
investigations to figure out whether a variable is set only once or
not. Only if it is sure that it can't change can it do these transformation.
For gcc, these investigations are only performed if you ask for optimization.
Thus, you _do_ get the same result if you ask for them:
g/x> c++ x.cc ; ./a.out
f(x) = 299
f(y) = 300
g/x> c++ x.cc -O2 ; ./a.out
f(x) = 300
f(y) = 300
In the first case, one computation is performed at compile- the other at
run-time. I understand that this seems weird here and maybe frustrating,
but there are good reasons for this.
W.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=15437