optimization/6330: tiny C++ prog broken under -O1
David Hanney
dh@digitalbrain.com
Mon Apr 22 05:46:00 GMT 2002
Many thanks for your prompt response!
I assumed (wrongly, I know) that because "the cast-output is the same four
bytes
as the cast-input" that it would be at the same address (union-style). My bad.
But, given this behavior-set ..
gcc with plain C++ works. (no compiler warning)
gcc with optimized C++ doesn't work. (no compiler warning)
gcc with C doesn't compile.
(under gcc 2.95.4 and gcc 3)
.. would a compiler warning be useful here?
Especially as the pointer may be formed implicitly via a reference
(as shown in the code quoted below)
cheers,
DH
-------------------------------------------
#!/bin/sh
# what compiler/OS version is it?
gcc-3.0 -v
uname -a
# create a program
echo '#include <stdio.h>' > x.cpp
echo '#include <stdlib.h>' >> x.cpp
echo 'typedef unsigned char byte;' >> x.cpp;
echo 'typedef byte *pbyte;' >> x.cpp;
echo 'void change(pbyte &z) { z = (pbyte)"correct"; }' >> x.cpp
echo 'int main() {' >> x.cpp
echo ' char *s = "incorrect";' >> x.cpp
echo ' change((byte*)s);' >> x.cpp
echo ' printf("%s\n", s);' >> x.cpp
echo ' exit(0);' >> x.cpp
echo '}' >> x.cpp
# compile and run it
gcc-3.0 x.cpp
./a.out
# compile and run The Same Program optimized
gcc-3.0 -O1 x.cpp
./a.out
-------------------------------------------
At 00:41 18/04/02 +0000, rth@gcc.gnu.org wrote:
>Synopsis: tiny C++ prog broken under -O1
>
>State-Changed-From-To: open->closed
>State-Changed-By: rth
>State-Changed-When: Wed Apr 17 17:41:35 2002
>State-Changed-Why:
> Not a bug. You have taken the address of a temporary.
> You wanted "change((byte**)&s)".
>
>http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=6330
>
More information about the Gcc-bugs
mailing list