This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: question - optimization and const
- From: John Love-Jensen <eljay at adobe dot com>
- To: Dima Sorkin <dima dot sorkin at gmail dot com>, MSX to GCC <gcc-help at gcc dot gnu dot org>
- Date: Wed, 15 Aug 2007 06:51:54 -0500
- Subject: Re: question - optimization and const
Hi Dima,
> 1) Does optimization process of g++ looks at "const"s
> at all ?
Yes. Some optimizations only happen with const data.
> 2) If yes, can the action of changing const arguments
> bring to incorrect code.
Yes.
For example, if the const data is stored in ROM, or protected RAM tagged as
Read Only, the code that modifies the data will fail.
Despite that the code leaves the data in its original state. (That is to
say, the data is logically const.)
You could represent the data this way:
struct Double10x10
{
mutable double data[10][10];
};
And pass in:
void Foo(const Double10x10& in);
Or pass it by value on the stack (which probably defeats what I presume is
your purpose not to copy the data in the first place... and also makes the
const qualifier of far lesser relevance):
void Foo(const Double10x10 in);
HTH,
--Eljay