Segmentation fault - interesting problem with array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sameer

    #1

    Segmentation fault - interesting problem with array

    Hi friends,
    I am using Mandriva Linux 9.2 and gcc.

    My source code is,

    int chunkin[7225][9] ; //no error
    int i ;
    for (i=0;i<7225;i++ )
    {
    chunkin[i][0] = somedata ;
    - -
    - -
    chunkin[i][8] = somedata ;
    }

    This gives Segmentation fault. Upto i = 5439 (5440 values ) it is
    working fine. After this it is giving Segmentation fault. Is this the
    maximum number of data an array can hold ? If so, What should I do for
    my array to hold 7225 * 9 data ([7225][9])

  • Artie Gold

    #2
    Re: Segmentation fault - interesting problem with array

    Sameer wrote:[color=blue]
    > Hi friends,
    > I am using Mandriva Linux 9.2 and gcc.[/color]

    Should not be relevant -- and if is, you're probably OT.
    [color=blue]
    >
    > My source code is,
    >
    > int chunkin[7225][9] ; //no error
    > int i ;
    > for (i=0;i<7225;i++ )
    > {
    > chunkin[i][0] = somedata ;
    > - -
    > - -
    > chunkin[i][8] = somedata ;
    > }
    >
    > This gives Segmentation fault. Upto i = 5439 (5440 values ) it is
    > working fine. After this it is giving Segmentation fault. Is this the
    > maximum number of data an array can hold ? If so, What should I do for
    > my array to hold 7225 * 9 data ([7225][9])
    >[/color]
    This looks fishy.

    Could you provide a compilable, runnable, snippet of code that exhibits
    this behavior?

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    I will (ir)regularly write about things that are important to me -- that I hope interest you. Of course, since I see most things as being political, that will be most of it.

    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"

    Comment

    • Albert

      #3
      Re: Segmentation fault - interesting problem with array

      Do you know how to view the right header file for maximum array sizes?
      And in terms of portability, it is something you should consider.
      I agree with Artie Gold; why don't you show a whole, complete program.

      Comment

      • Chuck F.

        #4
        Re: Segmentation fault - interesting problem with array

        Sameer wrote:[color=blue]
        >
        > int chunkin[7225][9] ; //no error
        > int i ;
        > for (i=0;i<7225;i++ )
        > {
        > chunkin[i][0] = somedata ;
        > - -
        > - -
        > chunkin[i][8] = somedata ;
        > }
        >
        > This gives Segmentation fault. Upto i = 5439 (5440 values ) it
        > is working fine. After this it is giving Segmentation fault. Is
        > this the maximum number of data an array can hold ? If so, What
        > should I do for my array to hold 7225 * 9 data ([7225][9])
        >[/color]
        My guess is that changing "int chunkin.." to "static int
        chunkin..." will cure it. In any case you are defining an object
        larger that what the C standard requires you system to provide. I
        think the required size is 65535 bytes.

        Most systems do better than this, but are usually limited in the
        amount of automatic storage they can provide. When you make the
        item static the system has a better chance of arranging for the
        storage to be present.

        Print out the value of "sizeof chunkin" (a size_t, not an integer)
        to get an idea of what you are asking for.

        --
        "If you want to post a followup via groups.google.c om, don't use
        the broken "Reply" link at the bottom of the article. Click on
        "show options" at the top of the article, then click on the
        "Reply" at the bottom of the article headers." - Keith Thompson
        More details at: <http://cfaj.freeshell. org/google/>

        Comment

        • Keith Thompson

          #5
          Re: Segmentation fault - interesting problem with array

          "Albert" <albert.xtheunk nown0@gmail.com > writes:[color=blue]
          > Do you know how to view the right header file for maximum array sizes?
          > And in terms of portability, it is something you should consider.
          > I agree with Artie Gold; why don't you show a whole, complete program.[/color]

          There is no standard header that specifies the maximum size of an
          array.

          And please read <http://cfaj.freeshell. org/google/> if you want us to
          know what you're talking about.

          --
          Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
          San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
          We must do something. This is something. Therefore, we must do this.

          Comment

          • Keith Thompson

            #6
            Re: Segmentation fault - interesting problem with array

            "Sameer" <ensameer@gmail .com> writes:[color=blue]
            > I am using Mandriva Linux 9.2 and gcc.
            >
            > My source code is,
            >
            > int chunkin[7225][9] ; //no error
            > int i ;
            > for (i=0;i<7225;i++ )
            > {
            > chunkin[i][0] = somedata ;
            > - -
            > - -
            > chunkin[i][8] = somedata ;
            > }
            >
            > This gives Segmentation fault. Upto i = 5439 (5440 values ) it is
            > working fine. After this it is giving Segmentation fault. Is this the
            > maximum number of data an array can hold ? If so, What should I do for
            > my array to hold 7225 * 9 data ([7225][9])[/color]

            It's not surprising that an implementation would impose a limit on the
            size of an automatic (typically stack-allocated) variable . It is
            surprising that it would allow you to declare such a variable, then
            blow up when you try to access it.

            But there could be some other problem in the code that you're not
            showing us. Post a real program, not a code snippet.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
            We must do something. This is something. Therefore, we must do this.

            Comment

            • tmp123

              #7
              Re: Segmentation fault - interesting problem with array


              Sameer wrote:[color=blue]
              > Hi friends,
              > I am using Mandriva Linux 9.2 and gcc.
              >
              > My source code is,
              >
              > int chunkin[7225][9] ; //no error
              > int i ;
              > for (i=0;i<7225;i++ )
              > {
              > chunkin[i][0] = somedata ;
              > - -
              > - -
              > chunkin[i][8] = somedata ;
              > }
              >
              > This gives Segmentation fault. Upto i = 5439 (5440 values ) it is
              > working fine. After this it is giving Segmentation fault. Is this the
              > maximum number of data an array can hold ? If so, What should I do for
              > my array to hold 7225 * 9 data ([7225][9])[/color]


              Hi,

              In the while someone answer you about the origin of this problem, there
              are two workarounds that you can try. Both are based on declare chunkin
              as int *chunkin[7229] and init it before use:

              Workaround 1:
              void init_chunk( void )
              {
              int i;
              for (i=0;i<7229;i++ ) chunkin[i]=calloc(9,sizeo f(chunkin[0][0]));
              }

              Woraround 2:
              int chunks1[2000][9];
              int chunks2[2000][9];
              int chunks3[2000][9];
              int chunks4[2000][9];
              int chunks5[2000][9];

              void init_chunk ( void )
              {
              for(i=0;i<2000; i++) chunkin[i]=chunks1[i];
              for(i=0;i<2000; i++) chunkin[2000+i]=chunks2[i];
              }

              (note: in the final code add the necessary constants, casts, ...)

              Kind regards.

              PS: Feel free of post this message if you want to receive comments
              about. However, hide my e-mail address.

              Comment

              • tmp123

                #8
                Re: Segmentation fault - interesting problem with array

                Sorry, this was a personal mail posted by mistake.

                However, taken into account it has been posted, anyone is free of made
                comments.

                Kind regards.

                Comment

                • Sameer

                  #9
                  Re: Segmentation fault - interesting problem with array

                  #include <stdio.h>

                  int ScanFile(int myimg[255][255])
                  {
                  FILE *fin, *ferosion ;
                  int j,i ;
                  int chunkin[7225][9], erosion[255][255] ;
                  int chc,chr,erosion centre ;
                  int sigma=0 ;
                  fin =
                  fopen("/home/sameer/Exercise/Cprog/ERDavies/newimg1bt70.img txt","r") ;
                  ferosion =
                  fopen("/home/sameer/Exercise/Cprog/ERDavies/erosion.sci","w ") ;

                  fprintf(ferosio n,"e=[\n") ;

                  for (j=0;j<255;j++) //row
                  {
                  for (i=0;i<255;i++) //col
                  {
                  fscanf(fin,"%d" ,&myimg[j][i]) ;
                  }
                  }

                  int chunk_no = 0, c1 = 0, r1 = 0 ;

                  for (chr=0; chr<85; chr++)
                  {
                  for (chc=0; chc<85; chc++)
                  {

                  chunkin[chunk_no][0] = myimg[1+r1][1+c1] ;
                  chunkin[chunk_no][1] = myimg[1+r1][2+c1] ;
                  chunkin[chunk_no][2] = myimg[0+r1][2+c1] ;
                  chunkin[chunk_no][3] = myimg[0+r1][1+c1] ;
                  chunkin[chunk_no][4] = myimg[0+r1][0+c1] ;
                  chunkin[chunk_no][5] = myimg[1+r1][0+c1] ;
                  chunkin[chunk_no][6] = myimg[2+r1][0+c1] ;
                  chunkin[chunk_no][7] = myimg[2+r1][1+c1] ;
                  chunkin[chunk_no][8] = myimg[2+r1][2+c1] ;

                  sigma =
                  chunkin[chunk_no][0]+chunkin[chunk_no][1]+chunkin[chunk_no][2]+chunkin[chunk_no][3]+chunkin[chunk_no][4]+chunkin[chunk_no][5]+chunkin[chunk_no][6]+chunkin[chunk_no][7]+chunkin[chunk_no][8]
                  ;
                  if (sigma < 9)
                  erosioncentre = 0 ;
                  else
                  erosioncentre = chunkin[chunk_no][0] ;

                  erosion[1+r1][1+c1] = erosioncentre ;
                  erosion[1+r1][2+c1] = chunkin[chunk_no][1] ;
                  erosion[0+r1][2+c1] = chunkin[chunk_no][2] ;
                  erosion[0+r1][1+c1] = chunkin[chunk_no][3] ;
                  erosion[0+r1][0+c1] = chunkin[chunk_no][4] ;
                  erosion[1+r1][0+c1] = chunkin[chunk_no][5] ;
                  erosion[2+r1][0+c1] = chunkin[chunk_no][6] ;
                  erosion[2+r1][1+c1] = chunkin[chunk_no][7] ;
                  erosion[2+r1][2+c1] = chunkin[chunk_no][8] ;

                  chunk_no+=1 ;
                  c1+=3 ;
                  //printf("chr=%d\ tchc=%d\n",chr, chc) ;

                  }
                  r1+=3 ;
                  }

                  for (r1=0; r1<255; r1++)
                  {
                  for (c1=0; c1<255; c1++)
                  {
                  fprintf(ferosio n,"%d\t",erosio n[r1][c1]) ;
                  }
                  fprintf(ferosio n,"%d\n") ;
                  }
                  fprintf(ferosio n,"];") ;

                  printf("%d\n",c hunkin[84][5]) ;

                  fclose(fin) ;
                  fclose(ferosion ) ;
                  return myimg ;
                  }

                  main()
                  {
                  int imgdata[255][255] ;

                  ScanFile(imgdat a) ;
                  printf("%d\n",i mgdata[251][0]) ;

                  }

                  ----------------
                  The code is above. I had declared int chunkin[7225][9]
                  And inside a for loop,
                  for (chr=0; chr<85; chr++)
                  {
                  for (chc=0; chc<85; chc++)
                  {
                  //coded above
                  }
                  }

                  I am getting datas for chunkin.
                  Once the values reaches, chr=63,chc=84 i am getting segmentation fault.

                  Comment

                  • Skarmander

                    #10
                    Re: Segmentation fault - interesting problem with array

                    Sameer wrote:[color=blue]
                    > #include <stdio.h>
                    >
                    > int ScanFile(int myimg[255][255])
                    > {
                    > FILE *fin, *ferosion ;
                    > int j,i ;
                    > int chunkin[7225][9], erosion[255][255] ;
                    > int chc,chr,erosion centre ;
                    > int sigma=0 ;
                    > fin =
                    > fopen("/home/sameer/Exercise/Cprog/ERDavies/newimg1bt70.img txt","r") ;
                    > ferosion =
                    > fopen("/home/sameer/Exercise/Cprog/ERDavies/erosion.sci","w ") ;
                    >
                    > fprintf(ferosio n,"e=[\n") ;
                    >
                    > for (j=0;j<255;j++) //row
                    > {
                    > for (i=0;i<255;i++) //col
                    > {
                    > fscanf(fin,"%d" ,&myimg[j][i]) ;
                    > }
                    > }
                    >
                    > int chunk_no = 0, c1 = 0, r1 = 0 ;
                    >
                    > for (chr=0; chr<85; chr++)
                    > {
                    > for (chc=0; chc<85; chc++)
                    > {
                    >
                    > chunkin[chunk_no][0] = myimg[1+r1][1+c1] ;
                    > chunkin[chunk_no][1] = myimg[1+r1][2+c1] ;
                    > chunkin[chunk_no][2] = myimg[0+r1][2+c1] ;
                    > chunkin[chunk_no][3] = myimg[0+r1][1+c1] ;
                    > chunkin[chunk_no][4] = myimg[0+r1][0+c1] ;
                    > chunkin[chunk_no][5] = myimg[1+r1][0+c1] ;
                    > chunkin[chunk_no][6] = myimg[2+r1][0+c1] ;
                    > chunkin[chunk_no][7] = myimg[2+r1][1+c1] ;
                    > chunkin[chunk_no][8] = myimg[2+r1][2+c1] ;
                    >
                    > sigma =
                    > chunkin[chunk_no][0]+chunkin[chunk_no][1]+chunkin[chunk_no][2]+chunkin[chunk_no][3]+chunkin[chunk_no][4]+chunkin[chunk_no][5]+chunkin[chunk_no][6]+chunkin[chunk_no][7]+chunkin[chunk_no][8]
                    > ;
                    > if (sigma < 9)
                    > erosioncentre = 0 ;
                    > else
                    > erosioncentre = chunkin[chunk_no][0] ;
                    >
                    > erosion[1+r1][1+c1] = erosioncentre ;
                    > erosion[1+r1][2+c1] = chunkin[chunk_no][1] ;
                    > erosion[0+r1][2+c1] = chunkin[chunk_no][2] ;
                    > erosion[0+r1][1+c1] = chunkin[chunk_no][3] ;
                    > erosion[0+r1][0+c1] = chunkin[chunk_no][4] ;
                    > erosion[1+r1][0+c1] = chunkin[chunk_no][5] ;
                    > erosion[2+r1][0+c1] = chunkin[chunk_no][6] ;
                    > erosion[2+r1][1+c1] = chunkin[chunk_no][7] ;
                    > erosion[2+r1][2+c1] = chunkin[chunk_no][8] ;
                    >
                    > chunk_no+=1 ;
                    > c1+=3 ;
                    > //printf("chr=%d\ tchc=%d\n",chr, chc) ;
                    >
                    > }
                    > r1+=3 ;
                    > }
                    >
                    > for (r1=0; r1<255; r1++)
                    > {
                    > for (c1=0; c1<255; c1++)
                    > {
                    > fprintf(ferosio n,"%d\t",erosio n[r1][c1]) ;
                    > }
                    > fprintf(ferosio n,"%d\n") ;
                    > }
                    > fprintf(ferosio n,"];") ;
                    >
                    > printf("%d\n",c hunkin[84][5]) ;
                    >
                    > fclose(fin) ;
                    > fclose(ferosion ) ;
                    > return myimg ;
                    > }
                    >
                    > main()
                    > {
                    > int imgdata[255][255] ;
                    >
                    > ScanFile(imgdat a) ;
                    > printf("%d\n",i mgdata[251][0]) ;
                    >
                    > }
                    >
                    > ----------------
                    > The code is above. I had declared int chunkin[7225][9]
                    > And inside a for loop,
                    > for (chr=0; chr<85; chr++)
                    > {
                    > for (chc=0; chc<85; chc++)
                    > {
                    > //coded above
                    > }
                    > }
                    >
                    > I am getting datas for chunkin.
                    > Once the values reaches, chr=63,chc=84 i am getting segmentation fault.
                    >[/color]

                    Hint: print the value of c1.

                    S.

                    Comment

                    • Dag-Erling Smørgrav

                      #11
                      Re: Segmentation fault - interesting problem with array

                      "Sameer" <ensameer@gmail .com> writes:[color=blue]
                      > Once the values reaches, chr=63,chc=84 i am getting segmentation fault.[/color]

                      the problem is not with chunkin. check the value of c1.

                      DES
                      --
                      Dag-Erling Smørgrav - des@des.no

                      Comment

                      • Dag-Erling Smørgrav

                        #12
                        Re: Segmentation fault - interesting problem with array

                        des@des.no (Dag-Erling Smørgrav) writes:[color=blue]
                        > "Sameer" <ensameer@gmail .com> writes:[color=green]
                        > > Once the values reaches, chr=63,chc=84 i am getting segmentation fault.[/color]
                        > the problem is not with chunkin. check the value of c1.[/color]

                        umm, I meant "the problem is not with chunk_no".

                        DES
                        --
                        Dag-Erling Smørgrav - des@des.no

                        Comment

                        • Targeur fou

                          #13
                          Re: Segmentation fault - interesting problem with array


                          Hello,

                          [snipped]
                          [color=blue]
                          >
                          > int chunk_no = 0, c1 = 0, r1 = 0 ;
                          >
                          > for (chr=0; chr<85; chr++)
                          > {[/color]

                          hint: something is perhaps needed here.
                          [color=blue]
                          > for (chc=0; chc<85; chc++)
                          > {
                          >
                          > chunkin[chunk_no][0] = myimg[1+r1][1+c1] ;
                          > chunkin[chunk_no][1] = myimg[1+r1][2+c1] ;[/color]

                          [snipped]

                          Regis

                          Comment

                          • M.B

                            #14
                            Re: Segmentation fault - interesting problem with array


                            Dag-Erling Smørgrav wrote:[color=blue]
                            > des@des.no (Dag-Erling Smørgrav) writes:[color=green]
                            > > "Sameer" <ensameer@gmail .com> writes:[color=darkred]
                            > > > Once the values reaches, chr=63,chc=84 i am getting segmentation fault.[/color]
                            > > the problem is not with chunkin. check the value of c1.[/color]
                            >
                            > umm, I meant "the problem is not with chunk_no".[/color]

                            problem is with c1 (it starts from 0 to 85*3*85 )
                            myimg is int[255][255]
                            at some point c1 goes over 255 and .......

                            possibly u forgot to reinitialize c1 to 0 in outer loop


                            [color=blue]
                            >
                            > DES
                            > --
                            > Dag-Erling Smørgrav - des@des.no[/color]

                            - M.B

                            Comment

                            • Richard Heathfield

                              #15
                              Re: Segmentation fault - interesting problem with array

                              tmp123 said:
                              [color=blue]
                              > Sorry, this was a personal mail posted by mistake.
                              >
                              > However, taken into account it has been posted, anyone is free of made
                              > comments.[/color]

                              The only comment I would make is that your advice was, alas, not very
                              helpful to the OP, even though you were trying to be helpful.

                              As you can see, others here have quickly identified the real problem. That's
                              the benefit of a newsgroup - lots of eyes, and lots of mutual peer review.

                              --
                              Richard Heathfield
                              "Usenet is a strange place" - dmr 29/7/1999

                              email: rjh at above domain (but drop the www, obviously)

                              Comment

                              Working...