Looping forever,can this be right?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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



[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux