C question: typecast changes behavior with optimizations enabled.

Adam Dickmeiss adam@indexdata.dk
Tue Sep 26 14:34:00 GMT 2006


Consider the attached which sweeps through an array of chars..

1) If a typecast is used (CAST defined), the *src is not updated and 
main will see (sz == 0).

2) If no typecast is used, *src is updated, and size == 1.

I expected that *src would be updated in both cases (hence the assert).. 
But I am probably wrong.. and would like to know why:-)

adam@peanut:~$ gcc -O3 o2tst.c  && ./a.out
a.out: o2tst.c:22: main: Assertion `sz == 1' failed.
Aborted
adam@peanut:~$ gcc -O2 o2tst.c  && ./a.out
a.out: o2tst.c:22: main: Assertion `sz == 1' failed.
Aborted
adam@peanut:~$ gcc -O o2tst.c  && ./a.out
adam@peanut:~$ gcc -v
Using built-in specs.
Target: i486-linux-gnu
Configured with: ../src/configure -v 
--enable-languages=c,c++,fortran,objc,obj-c++,treelang --prefix=/usr 
--enable-shared --with-system-zlib --libexecdir=/usr/lib 
--without-included-gettext --enable-threads=posix --enable-nls 
--program-suffix=-4.1 --enable-__cxa_atexit --enable-clocale=gnu 
--enable-libstdcxx-debug --enable-mpfr --with-tune=i686 
--enable-checking=release i486-linux-gnu
Thread model: posix
gcc version 4.1.2 20060901 (prerelease) (Debian 4.1.1-13)

Same behavior on
adam@stauning:~/proj/idzebra-2.0.2/isamb$ gcc -v
Reading specs from /usr/local/lib/gcc/sparc-sun-solaris2.9/3.4.6/specs
Configured with: ../configure --with-as=/usr/ccs/bin/as 
--with-ld=/usr/ccs/bin/ld --enable-shared --enable-languages=c,c++,f77
Thread model: posix
gcc version 3.4.6

/ Adam
-------------- next part --------------
A non-text attachment was scrubbed...
Name: o2tst.c
Type: text/x-csrc
Size: 372 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/gcc/attachments/20060926/f62f89a0/attachment.bin>


More information about the Gcc mailing list