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:-)

[EMAIL PROTECTED]:~$ gcc -O3 o2tst.c  && ./a.out
a.out: o2tst.c:22: main: Assertion `sz == 1' failed.
Aborted
[EMAIL PROTECTED]:~$ gcc -O2 o2tst.c  && ./a.out
a.out: o2tst.c:22: main: Assertion `sz == 1' failed.
Aborted
[EMAIL PROTECTED]:~$ gcc -O o2tst.c  && ./a.out
[EMAIL PROTECTED]:~$ 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
[EMAIL PROTECTED]:~/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
#include <stdio.h>
#include <assert.h>

#define CAST (const unsigned char **)
// #define CAST 

static void decode_ptr(const char **src)
{
    unsigned char c;
    while ((c = *(* CAST src)++))
	;
}

int main(int argc, char **argv)
{
    const char *src0 = "";
    const char *src = src0;
    size_t sz;

    decode_ptr(&src);
    sz = src - src0;
    assert(sz == 1);
}

Reply via email to