Segmentation fault - interesting problem with array

Discussion in 'C Programming' started by Sameer, Jan 3, 2006.

  1. Sameer

    Sameer Guest

    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[0] = somedata ;
    - -
    - -
    chunkin[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])
     
    Sameer, Jan 3, 2006
    #1
    1. Advertising

  2. Sameer

    Artie Gold Guest

    Sameer wrote:
    > Hi friends,
    > I am using Mandriva Linux 9.2 and gcc.


    Should not be relevant -- and if is, you're probably OT.

    >
    > My source code is,
    >
    > int chunkin[7225][9] ; //no error
    > int i ;
    > for (i=0;i<7225;i++)
    > {
    > chunkin[0] = somedata ;
    > - -
    > - -
    > chunkin[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])
    >

    This looks fishy.

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

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
    http://goldsays.blogspot.com
    http://www.cafepress.com/goldsays
    "If you have nothing to hide, you're not trying!"
     
    Artie Gold, Jan 3, 2006
    #2
    1. Advertising

  3. Sameer

    Albert Guest

    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.
     
    Albert, Jan 3, 2006
    #3
  4. Sameer

    Chuck F. Guest

    Sameer wrote:
    >
    > int chunkin[7225][9] ; //no error
    > int i ;
    > for (i=0;i<7225;i++)
    > {
    > chunkin[0] = somedata ;
    > - -
    > - -
    > chunkin[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])
    >

    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.com, 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/>
     
    Chuck F., Jan 3, 2006
    #4
  5. "Albert" <> writes:
    > 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.


    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_Keith) <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.
     
    Keith Thompson, Jan 3, 2006
    #5
  6. "Sameer" <> writes:
    > 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[0] = somedata ;
    > - -
    > - -
    > chunkin[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])


    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_Keith) <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.
     
    Keith Thompson, Jan 3, 2006
    #6
  7. Sameer

    tmp123 Guest

    Sameer wrote:
    > 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[0] = somedata ;
    > - -
    > - -
    > chunkin[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])



    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=calloc(9,sizeof(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=chunks1;
    for(i=0;i<2000;i++) chunkin[2000+i]=chunks2;
    }

    (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.
     
    tmp123, Jan 3, 2006
    #7
  8. Sameer

    tmp123 Guest

    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.
     
    tmp123, Jan 3, 2006
    #8
  9. Sameer

    Sameer Guest

    #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,erosioncentre ;
    int sigma=0 ;
    fin =
    fopen("/home/sameer/Exercise/Cprog/ERDavies/newimg1bt70.imgtxt","r") ;
    ferosion =
    fopen("/home/sameer/Exercise/Cprog/ERDavies/erosion.sci","w") ;

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

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

    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(ferosion,"%d\t",erosion[r1][c1]) ;
    }
    fprintf(ferosion,"%d\n") ;
    }
    fprintf(ferosion,"];") ;

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

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

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

    ScanFile(imgdata) ;
    printf("%d\n",imgdata[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.
     
    Sameer, Jan 3, 2006
    #9
  10. Sameer

    Skarmander Guest

    Sameer wrote:
    > #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,erosioncentre ;
    > int sigma=0 ;
    > fin =
    > fopen("/home/sameer/Exercise/Cprog/ERDavies/newimg1bt70.imgtxt","r") ;
    > ferosion =
    > fopen("/home/sameer/Exercise/Cprog/ERDavies/erosion.sci","w") ;
    >
    > fprintf(ferosion,"e=[\n") ;
    >
    > for (j=0;j<255;j++) //row
    > {
    > for (i=0;i<255;i++) //col
    > {
    > fscanf(fin,"%d",&myimg[j]) ;
    > }
    > }
    >
    > 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(ferosion,"%d\t",erosion[r1][c1]) ;
    > }
    > fprintf(ferosion,"%d\n") ;
    > }
    > fprintf(ferosion,"];") ;
    >
    > printf("%d\n",chunkin[84][5]) ;
    >
    > fclose(fin) ;
    > fclose(ferosion) ;
    > return myimg ;
    > }
    >
    > main()
    > {
    > int imgdata[255][255] ;
    >
    > ScanFile(imgdata) ;
    > printf("%d\n",imgdata[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.
    >


    Hint: print the value of c1.

    S.
     
    Skarmander, Jan 3, 2006
    #10
  11. "Sameer" <> writes:
    > Once the values reaches, chr=63,chc=84 i am getting segmentation fault.


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

    DES
    --
    Dag-Erling Smørgrav -
     
    =?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=, Jan 3, 2006
    #11
  12. (Dag-Erling Smørgrav) writes:
    > "Sameer" <> writes:
    > > Once the values reaches, chr=63,chc=84 i am getting segmentation fault.

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


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

    DES
    --
    Dag-Erling Smørgrav -
     
    =?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=, Jan 3, 2006
    #12
  13. Sameer

    Targeur fou Guest

    Hello,

    [snipped]

    >
    > int chunk_no = 0, c1 = 0, r1 = 0 ;
    >
    > for (chr=0; chr<85; chr++)
    > {


    hint: something is perhaps needed here.

    > for (chc=0; chc<85; chc++)
    > {
    >
    > chunkin[chunk_no][0] = myimg[1+r1][1+c1] ;
    > chunkin[chunk_no][1] = myimg[1+r1][2+c1] ;


    [snipped]

    Regis
     
    Targeur fou, Jan 3, 2006
    #13
  14. Sameer

    M.B Guest

    Dag-Erling Smørgrav wrote:
    > (Dag-Erling Smørgrav) writes:
    > > "Sameer" <> writes:
    > > > Once the values reaches, chr=63,chc=84 i am getting segmentation fault.

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

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


    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



    >
    > DES
    > --
    > Dag-Erling Smørgrav -


    - M.B
     
    M.B, Jan 3, 2006
    #14
  15. tmp123 said:

    > Sorry, this was a personal mail posted by mistake.
    >
    > However, taken into account it has been posted, anyone is free of made
    > comments.


    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
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Jan 3, 2006
    #15
  16. Sameer a écrit :
    > int chunkin[7225][9] ; //no error
    > int i ;
    > for (i=0;i<7225;i++)
    > {
    > chunkin[0] = somedata ;
    > - -
    > - -
    > chunkin[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])


    This is a big object for the automatic memory. You probably have some
    undefined behaviour due to a lack of automatic memory. The problem is
    that there is no way to prevent this.

    Try 'static', just for the fun. In the real world, pass the address of a
    static array or use dynamic allocation (a pointer to array can help in
    such a case).

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Jan 3, 2006
    #16
  17. Sameer a écrit :
    > int ScanFile(int myimg[255][255])
    > {
    > FILE *fin, *ferosion ;
    > int j,i ;
    > int chunkin[7225][9], erosion[255][255] ;


    Gee! The beast was not the only one ! Stop declaring so big objects on
    the automatic memory. It was not designed for that. Use static or allocated.

    --
    A+

    Emmanuel Delahaye
     
    Emmanuel Delahaye, Jan 3, 2006
    #17
  18. Sameer

    Sameer Guest

    Hi,
    It is working fine now. I forgot to re-initialize c1=0 in the outer
    for loop ,
    for (chr=0; chr<85; chr++)
    {
    c1=0 ; //this is what i was missing.
    for (chc=0; chc<85; chc++)
    {

    Thank you M.B, Skarmander, Dag-Erling Smørgrav, Targeur fou and all
    others.

    Now another doubt has arised. Without initializing c1=0 in the outer
    loop, the value of c1 went upto 16317. I have assigned only 255 to
    myimg i.e myimg[255][255] Then Why is it so, it can go upto
    myimg[192][16317] ?
     
    Sameer, Jan 4, 2006
    #18
  19. Sameer

    Flash Gordon Guest

    Sameer wrote:
    > Hi,
    > It is working fine now. I forgot to re-initialize c1=0 in the outer
    > for loop ,
    > for (chr=0; chr<85; chr++)
    > {
    > c1=0 ; //this is what i was missing.
    > for (chc=0; chc<85; chc++)
    > {
    >
    > Thank you M.B, Skarmander, Dag-Erling Smørgrav, Targeur fou and all
    > others.
    >
    > Now another doubt has arised. Without initializing c1=0 in the outer
    > loop, the value of c1 went upto 16317. I have assigned only 255 to
    > myimg i.e myimg[255][255] Then Why is it so, it can go upto
    > myimg[192][16317] ?


    Blind luck. Whether it is good or bad luck is a matter of opinion.

    Standard C answer: When you write off the end of an array you invoke
    undefined behaviour. Undefined behaviour means that, from a C language
    perspective, *anything* can happen, including it appearing to work.

    Implementation specific answer: There happened to be that much memory
    you could access without it causing symptoms that were visible to you.
    This memory might or might not have been unused.
    --
    Flash Gordon
    Living in interesting times.
    Although my email address says spam, it is real and I read it.
     
    Flash Gordon, Jan 4, 2006
    #19
  20. Sameer

    M.B Guest

    Sameer wrote:
    > Hi,
    > It is working fine now. I forgot to re-initialize c1=0 in the outer
    > for loop ,
    > for (chr=0; chr<85; chr++)
    > {
    > c1=0 ; //this is what i was missing.
    > for (chc=0; chc<85; chc++)
    > {
    >
    > Thank you M.B, Skarmander, Dag-Erling Smørgrav, Targeur fou and all
    > others.
    >
    > Now another doubt has arised. Without initializing c1=0 in the outer
    > loop, the value of c1 went upto 16317. I have assigned only 255 to
    > myimg i.e myimg[255][255] Then Why is it so, it can go upto
    > myimg[192][16317] ?


    That was your good lick.

    anyway you can access an array by any index until its valid for system
    (within memory segment boundry - i guess thats the term?!. even if the
    index is negative dont matter. ).its up to you to take care of those.
    as you have mentioned that you are using Linux, tools like electric
    fence can detect this invalid memory access for any array.

    -M.B
     
    M.B, Jan 4, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Goh, Yong Kwang
    Replies:
    3
    Views:
    11,876
    Barry Schwarz
    Apr 3, 2004
  2. esanchezfo

    Segmentation fault char array pointer

    esanchezfo, Jun 1, 2006, in forum: C Programming
    Replies:
    1
    Views:
    457
    Fred Kleinschmidt
    Jun 1, 2006
  3. Ben
    Replies:
    8
    Views:
    488
  4. SP
    Replies:
    8
    Views:
    755
    Dave Thompson
    Aug 14, 2006
  5. b3hzat
    Replies:
    2
    Views:
    650
    Dann Corbit
    Jul 21, 2010
Loading...

Share This Page