Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Segmentation fault - interesting problem with array

Reply
Thread Tools

Segmentation fault - interesting problem with array

 
 
Sameer
Guest
Posts: n/a
 
      01-03-2006
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])

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      01-03-2006
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[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])
>

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!"
 
Reply With Quote
 
 
 
 
Albert
Guest
Posts: n/a
 
      01-03-2006
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.

 
Reply With Quote
 
Chuck F.
Guest
Posts: n/a
 
      01-03-2006
Sameer wrote:
>
> 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])
>

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/>
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-03-2006
"Albert" <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      01-03-2006
"Sameer" <(E-Mail Removed)> 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[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])


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) (E-Mail Removed) <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.
 
Reply With Quote
 
tmp123
Guest
Posts: n/a
 
      01-03-2006

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[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])



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,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[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.

 
Reply With Quote
 
tmp123
Guest
Posts: n/a
 
      01-03-2006
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.

 
Reply With Quote
 
Sameer
Guest
Posts: n/a
 
      01-03-2006
#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][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(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.

 
Reply With Quote
 
Skarmander
Guest
Posts: n/a
 
      01-03-2006
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][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(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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Interesting segmentation fault, I cannot find root cause b3hzat C Programming 2 07-21-2010 12:20 AM
segmentation fault writing to array elements in structure SP C Programming 8 08-14-2006 06:59 AM
Help with array/pointer segmentation fault needed Ben C Programming 8 06-13-2006 07:10 AM
Segmentation fault char array pointer esanchezfo C Programming 1 06-01-2006 05:17 PM
Pointer confusion with passing string array -- Segmentation fault Goh, Yong Kwang C Programming 3 04-03-2004 04:55 PM



Advertisments