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]) | | | | 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 http://goldsays.blogspot.com http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!" | | | | 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. | | | | 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.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/> | | | | re: Segmentation fault - interesting problem with array
"Albert" <albert.xtheunknown0@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_Keith) 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. | | | | 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_Keith) 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. | | | | 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,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. | | | | 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. | | | | 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,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. | | | | 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,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.
>[/color]
Hint: print the value of c1.
S. | | | | 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 | | | | 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 | | | | 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 | | | | 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 | | | | 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 http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously) | | | | re: Segmentation fault - interesting problem with array
Sameer a écrit :[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]
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 | | | | re: Segmentation fault - interesting problem with array
Sameer a écrit :[color=blue]
> int ScanFile(int myimg[255][255])
> {
> FILE *fin, *ferosion ;
> int j,i ;
> int chunkin[7225][9], erosion[255][255] ;[/color]
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 | | | | re: Segmentation fault - interesting problem with array
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] ? | | | | re: Segmentation fault - interesting problem with array
Sameer wrote:[color=blue]
> 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] ?[/color]
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. | | | | re: Segmentation fault - interesting problem with array
Sameer wrote:[color=blue]
> 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] ?[/color]
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 |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|