Segmentation fault - interesting problem with array

S

Sameer

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

Artie Gold

Sameer said:
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
 
A

Albert

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.
 
C

Chuck F.

Sameer said:
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/>
 
K

Keith Thompson

Albert said:
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.
 
K

Keith Thompson

Sameer said:
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.
 
T

tmp123

Sameer said:
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.
 
T

tmp123

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.
 
S

Sameer

#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.
 
S

Skarmander

Sameer said:
#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.
 
?

=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=

Sameer said:
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
 
T

Targeur fou

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
 
M

M.B

Dag-Erling Smørgrav said:
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



- M.B
 
R

Richard Heathfield

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.
 
E

Emmanuel Delahaye

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).
 
E

Emmanuel Delahaye

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.
 
S

Sameer

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] ?
 
F

Flash Gordon

Sameer said:
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.
 
M

M.B

Sameer said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top