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

 
 
=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=
Guest
Posts: n/a
 
      01-03-2006
"Sameer" <(E-Mail Removed)> 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 - http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
=?iso-8859-1?q?Dag-Erling_Sm=F8rgrav?=
Guest
Posts: n/a
 
      01-03-2006
(E-Mail Removed) (Dag-Erling Smørgrav) writes:
> "Sameer" <(E-Mail Removed)> 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 - (E-Mail Removed)
 
Reply With Quote
 
 
 
 
Targeur fou
Guest
Posts: n/a
 
      01-03-2006

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

 
Reply With Quote
 
M.B
Guest
Posts: n/a
 
      01-03-2006

Dag-Erling Smørgrav wrote:
> (E-Mail Removed) (Dag-Erling Smørgrav) writes:
> > "Sameer" <(E-Mail Removed)> 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 - (E-Mail Removed)


- M.B

 
Reply With Quote
 
Richard Heathfield
Guest
Posts: n/a
 
      01-03-2006
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)
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-03-2006
Sameer a écrit :
> 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 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
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-03-2006
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
 
Reply With Quote
 
Sameer
Guest
Posts: n/a
 
      01-04-2006
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] ?

 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      01-04-2006
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.
 
Reply With Quote
 
M.B
Guest
Posts: n/a
 
      01-04-2006

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

 
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