This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
C++ standards question
- From: John Fine <johnsfine at verizon dot net>
- To: gcc-help <gcc-help at gcc dot gnu dot org>
- Date: Sun, 03 Feb 2013 18:24:28 -0500
- Subject: C++ standards question
I have a construct that the optimizer in VS2010 64-bit changes to
something that doesn't work.
I want to know whether this is a bug in the compiler or in the code
being compiled.
I would have no idea where to look in the standards document for this
answer. I know it is off-topic (a C++ not gcc question) but this is
where the experts are.
My function takes a parameter char* p
One of the things the function does is test and conditionally modifies a
char pointed to through P. Something like:
if ( expression of *p ) *p = '-';
The optimizer changes that to the equivalent of
*p = (expression of *p ) ? '-' : *p;
Is that a valid optimization?
The function is called sometimes with a pointer to read-only memory and
sometimes with a pointer to read/write memory.
When the pointer points to read only memory, that expression of *p will
always be false (that part works), so the write does not occur in
unoptimized code.
When the pointer points to read/write memory, that expression of *p is
usually false but might be true, so the write occasionally occurs.
But in the optimized code that byte is always written. It is usually
written with a copy of the value read from in during the evaluation of
the test expression.
BTW, I have seen code like the following get optimized in a similar way,
and that seems to me to be a reliably correct optimization:
*p = 'x'; if ( some expression) *p='y';
that gets correctly optimized to
*p = ( some expression ) ? 'y' : 'x';
That is safe because the original code always wrote to that location and
the optimized code also always writes.