optimization/6330: tiny C++ prog broken under -O1
dh@digitalbrain.com
dh@digitalbrain.com
Wed Apr 17 02:52:00 GMT 2002
>Number: 6330
>Category: optimization
>Synopsis: tiny C++ prog broken under -O1
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: sw-bug
>Submitter-Id: net
>Arrival-Date: Wed Apr 17 02:16:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator: dh@digitalbrain.com
>Release: 2.95.4 20011002 (Debian prerelease)
>Organization:
>Environment:
gcc version 2.95.4 20011002 (Debian prerelease)
Linux fee 2.4.18-p7-rmap12c-r #267 SMP Mon Feb 4 12:05:56 GMT 2002 i686 unknown
>Description:
Taking the address of an automatic variable says to the compiler "don't use a register for this!".
But I suspect that casting an automatic variable into another type and then taking the address of That
doesn't say anything of the kind
(even though it should)
>How-To-Repeat:
this linux console output shows a tiny shell script
being run that duplicates the problem ..
5750:fee:~: cat ./show_gcc_bug.sh
#!/bin/sh
# what compiler/OS version is it?
gcc -v
uname -a
# create a program
echo '#include <stdio.h>' > x.cpp
echo 'typedef unsigned char byte;' >> x.cpp;
echo 'void change(byte **z) { *z = (byte*)"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 x.cpp
./a.out
# compile and run The Same Program optimized
gcc -O1 x.cpp
./a.out
5751:fee:~: ./show_gcc_bug.sh
Reading specs from /usr/lib/gcc-lib/i386-linux/2.95.4/specs
gcc version 2.95.4 20011002 (Debian prerelease)
Linux fee 2.4.18-p7-rmap12c-r #267 SMP Mon Feb 4 12:05:56 GMT 2002 i686 unknown
correct
incorrect
5752:fee:~:
>Fix:
no fix known ..
work arounds:
1)
change the x.cpp from &(byte*)s
to (byte**)&s
2)
rename x.cpp to x.c
because the C front end will give the error
"x.c:6: invalid lvalue in unary `&'"
rather than make incorrect code
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the Gcc-bugs
mailing list