It appears that the address of a[11] coincides with the variable i on the stack, so when you execute a[11]=1, i is reset to 1, and it keeps looping forever. That's why you see a[2] printed after a[11], because a[11]=1 sets i to 1, and the for loop increments i to 2. When you set a[11]=11, the loop continues by incrementing i to 12, and moves on writing over to the rest of the stack. On 3/11/06, Petur Thors <peturth@xxxxx> wrote: > Hi > Please correct me if I'm totally lost here , but compiling this program > (a.c) with gcc -Wall -pedantic -o a a.c : > > #include <stdio.h> > int main(int argc,char **argv) > { > int i; > int a[10]; > for(i = 0; i < 2000; i++) { > printf("a[%d] is %d\n",i,a[i]); > a[i] = 1; > } > return 0; > } > > > and running it i get : > > a[0] is 0 > a[1] is 0 > a[2] is -2146329632 > a[3] is 51 > a[4] is 4195584 > a[5] is 0 > a[6] is 4195312 > a[7] is 0 > a[8] is -4327808 > a[9] is 32767 > a[10] is 0 > a[11] is 11 > a[2] is 1 > a[3] is 1 > a[4] is 1 > a[5] is 1 > a[6] is 1 > a[7] is 1 > a[8] is 1 > a[9] is 1 > a[10] is 1 > a[11] is 11 > a[2] is 1 > a[3] is 1 > a[4] is 1 > a[5] is 1 > a[6] is 1 > a[7] is 1 > .... > looping forever and ever........ > > But when I change a[i] = 1 to a[i] = 11 (in the for loop) it segfaults > at i = 1532. > > Please forgive me if I'm totally missing something obvious here but can > this be correct ? > > gcc specs: > > Using built-in specs. > Target: x86_64-redhat-linux > Configured with: ../configure --prefix=/usr --mandir=/usr/share/man > --infodir=/usr/share/info --enable-shared --enable-threads=posix > --enable-checking=release --with-system-zlib --enable-__cxa_atexit > --disable-libunwind-exceptions --enable-libgcj-multifile > --enable-languages=c,c++,objc,java,f95,ada --enable-java-awt=gtk > --with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre > --host=x86_64-redhat-linux > Thread model: posix > gcc version 4.0.2 20051125 (Red Hat 4.0.2-8) > Updated Fedora core 4 > > Thanks in advance and best regards > Petur > > >