Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.c

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 05-14-2012, 04:45 PM
pembed2012
Guest
 
Posts: n/a
Default Segmentation Fault

I am sure that I am missing something but I cannot get the following
code to work. It doesn't even make it to my test printf statement
before I get a segmentation fault. Does anyone have any ideas why I
can't get this to run?


void
srtn (int atm[10], int ctm[10])
{
int rng = 0;
int qu[15];
int wt[11];
int tarnd[11];
int i, j;
printf ("This is a test");
for(i = 0; i < 11; i++)
{
wt[i] = 0;
tarnd[i] = 0;
}
qu[0] = 0;
qu[1] = -1;
int size = 1;
i = 0;
while(qu[i] != -1)
{
ctm[qu[i]]--;
rng++;
for(j = i + 1; qu[j] != -1; j++)
wt[qu[j]]++;
for(j = 0; j < 10; j++)
if(atm[j] != -1)
if(atm[j] <= rng)
{
qu[size] = j;
qu[size + 1] = -1;
size++;
}
tarnd[qu[i]]++;
if(ctm[qu[i]] == 0);
atm[qu[i]] = -1;
for(j = 0; j < size; j++)
if(atm[qu[j]] != -1)
if(ctm[qu[j]] < ctm[qu[i]])
i = j;
}
for(i = 0; i < 10; i++)
tarnd[i] += wt[i];
for(i = 0; i < 10; i++)
{
wt[10] += wt[i];
tarnd[10] += tarnd[i];
}
wt[10] = wt[10] / 10;
tarnd[10] = tarnd[10] / 10;

print_report (wt, tarnd);
}

Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 05-14-2012, 05:00 PM
John Gordon
Guest
 
Posts: n/a
Default Re: Segmentation Fault

In <jorcqt$72h$1@speranza.aioe.org> pembed2012 <once@was.enough.invalid> writes:

> I am sure that I am missing something but I cannot get the following
> code to work. It doesn't even make it to my test printf statement
> before I get a segmentation fault. Does anyone have any ideas why I
> can't get this to run?


The print statement doesn't show up because it doesn't end in a newline.
Try adding a newline, like so:

printf ("This is a test\n");

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Reply With Quote
  #3 (permalink)  
Old 05-14-2012, 05:02 PM
Fred K
Guest
 
Posts: n/a
Default Re: Segmentation Fault

On Monday, May 14, 2012 9:45:17 AM UTC-7, pembed2012 wrote:
> I am sure that I am missing something but I cannot get the following
> code to work. It doesn't even make it to my test printf statement
> before I get a segmentation fault. Does anyone have any ideas why I
> can't get this to run?
>
>
> void
> srtn (int atm[10], int ctm[10])
> {
> int rng = 0;
> int qu[15];
> int wt[11];
> int tarnd[11];
> int i, j;
> printf ("This is a test");
> for(i = 0; i < 11; i++)
> {
> wt[i] = 0;
> tarnd[i] = 0;
> }
> qu[0] = 0;
> qu[1] = -1;
> int size = 1;
> i = 0;
> while(qu[i] != -1)
> {
> ctm[qu[i]]--;
> rng++;
> for(j = i + 1; qu[j] != -1; j++)
> wt[qu[j]]++;
> for(j = 0; j < 10; j++)
> if(atm[j] != -1)
> if(atm[j] <= rng)
> {
> qu[size] = j;
> qu[size + 1] = -1;
> size++;
> }
> tarnd[qu[i]]++;
> if(ctm[qu[i]] == 0);
> atm[qu[i]] = -1;
> for(j = 0; j < size; j++)
> if(atm[qu[j]] != -1)
> if(ctm[qu[j]] < ctm[qu[i]])
> i = j;
> }
> for(i = 0; i < 10; i++)
> tarnd[i] += wt[i];
> for(i = 0; i < 10; i++)
> {
> wt[10] += wt[i];
> tarnd[10] += tarnd[i];
> }
> wt[10] = wt[10] / 10;
> tarnd[10] = tarnd[10] / 10;
>
> print_report (wt, tarnd);
> }


The problem seems to be in the code you didn't show us.
Reply With Quote
  #4 (permalink)  
Old 05-14-2012, 05:25 PM
James Kuyper
Guest
 
Posts: n/a
Default Re: Segmentation Fault

On 05/14/2012 12:45 PM, pembed2012 wrote:
> I am sure that I am missing something but I cannot get the following
> code to work. It doesn't even make it to my test printf statement
> before I get a segmentation fault. Does anyone have any ideas why I
> can't get this to run?


The reason why the test printf() produces no visible effects before the
segmentation fault has already been provided by John Gordon. For such
debugging printouts, I generally prefer fprintf(stderr, format,
arguments) over printf(format, arguments), to avoid such problems.

There might be some fairly obvious reason why the segfault is occurring
(I've only taken a quick glance at it) but without knowing the context,
it could be very difficult to locate the problem. It would make things a
lot easier for those interested in helping you, if you could provide a
complete, simple program demonstrating the problem. That program should
set atm and ctm to point at appropriately initialized arrays. It would
also be helpful to know precisely which compiler you're using, and with
what options turned on.
Reply With Quote
  #5 (permalink)  
Old 05-14-2012, 06:33 PM
Fred K
Guest
 
Posts: n/a
Default Re: Segmentation Fault

On Monday, May 14, 2012 9:45:17 AM UTC-7, pembed2012 wrote:
> I am sure that I am missing something but I cannot get the following
> code to work. It doesn't even make it to my test printf statement
> before I get a segmentation fault. Does anyone have any ideas why I
> can't get this to run?
>
>
> void
> srtn (int atm[10], int ctm[10])
> {
> int rng = 0;
> int qu[15];
> int wt[11];
> int tarnd[11];
> int i, j;
> printf ("This is a test");
> for(i = 0; i < 11; i++)
> {
> wt[i] = 0;
> tarnd[i] = 0;
> }
> qu[0] = 0;
> qu[1] = -1;
> int size = 1;
> i = 0;
> while(qu[i] != -1)
> {
> ctm[qu[i]]--;
> rng++;
> for(j = i + 1; qu[j] != -1; j++)
> wt[qu[j]]++;
> for(j = 0; j < 10; j++)
> if(atm[j] != -1)
> if(atm[j] <= rng)
> {
> qu[size] = j;


You have no protection here for what happens
if 'size' ever gets larger than 13. If it
does, then this next statement overwrites the
bounds of qu.


> qu[size + 1] = -1;
> size++;
> }
> tarnd[qu[i]]++;
> if(ctm[qu[i]] == 0);
> atm[qu[i]] = -1;
> for(j = 0; j < size; j++)
> if(atm[qu[j]] != -1)
> if(ctm[qu[j]] < ctm[qu[i]])
> i = j;


With the above nested for/if/if you would
do well to use braces to delimit the loop
and 'if' tests. Note that you can keep
replacing i here - is that what you want?
Or do you want to exit the loop as soon as
you set i ? (I did not try to follow your
algorithm, but if you really want to use
the last j that fits, why not start with
size and work backwards, exiting on the first
set of i ?)

}
> for(i = 0; i < 10; i++)
> tarnd[i] += wt[i];
> for(i = 0; i < 10; i++)
> {
> wt[10] += wt[i];
> tarnd[10] += tarnd[i];
> }
> wt[10] = wt[10] / 10;
> tarnd[10] = tarnd[10] / 10;
>
> print_report (wt, tarnd);
> }


Reply With Quote
  #6 (permalink)  
Old 05-14-2012, 08:44 PM
Keith Thompson
Guest
 
Posts: n/a
Default Re: Segmentation Fault

John Gordon <gordon@panix.com> writes:
> In <jorcqt$72h$1@speranza.aioe.org> pembed2012 <once@was.enough.invalid> writes:
>> I am sure that I am missing something but I cannot get the following
>> code to work. It doesn't even make it to my test printf statement
>> before I get a segmentation fault. Does anyone have any ideas why I
>> can't get this to run?

>
> The print statement doesn't show up because it doesn't end in a newline.
> Try adding a newline, like so:
>
> printf ("This is a test\n");


And it couldn't hurt to add

fflush(stdout);

as well.

--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Reply With Quote
  #7 (permalink)  
Old 05-14-2012, 09:00 PM
pembed2012
Guest
 
Posts: n/a
Default Re: Segmentation Fault

On Mon, 14 May 2012 13:25:58 -0400, James Kuyper wrote:

> On 05/14/2012 12:45 PM, pembed2012 wrote:
>> I am sure that I am missing something but I cannot get the following
>> code to work. It doesn't even make it to my test printf statement
>> before I get a segmentation fault. Does anyone have any ideas why I
>> can't get this to run?

>
> The reason why the test printf() produces no visible effects before the
> segmentation fault has already been provided by John Gordon. For such
> debugging printouts, I generally prefer fprintf(stderr, format,
> arguments) over printf(format, arguments), to avoid such problems.
>
> There might be some fairly obvious reason why the segfault is occurring
> (I've only taken a quick glance at it) but without knowing the context,
> it could be very difficult to locate the problem. It would make things a
> lot easier for those interested in helping you, if you could provide a
> complete, simple program demonstrating the problem. That program should
> set atm and ctm to point at appropriately initialized arrays. It would
> also be helpful to know precisely which compiler you're using, and with
> what options turned on.


Thanks everyone, you are right. Putting \n means the printf fires, so the
segmentation fault happens later.

Greatful if anyone is able to do a full debugging of this function.
Basically it's a kind of queue finding waiting and turnaround times. I
don't know exactly how it's working.

Compiler is G++, version 3.2.2, default options.

Thanks
Reply With Quote
  #8 (permalink)  
Old 05-14-2012, 09:07 PM
Edward Rutherford
Guest
 
Posts: n/a
Default Re: Segmentation Fault

Keith Thompson wrote:

> John Gordon <gordon@panix.com> writes:
>> In <jorcqt$72h$1@speranza.aioe.org> pembed2012
>> <once@was.enough.invalid> writes:
>>> I am sure that I am missing something but I cannot get the following
>>> code to work. It doesn't even make it to my test printf statement
>>> before I get a segmentation fault. Does anyone have any ideas why I
>>> can't get this to run?

>>
>> The print statement doesn't show up because it doesn't end in a
>> newline. Try adding a newline, like so:
>>
>> printf ("This is a test\n");

>
> And it couldn't hurt to add
>
> fflush(stdout);
>
> as well.


It couldn't hurt but it wouldn't help either, since stdout gets flushed
automatically at a new-line!

//EPR
Reply With Quote
  #9 (permalink)  
Old 05-14-2012, 09:10 PM
John Gordon
Guest
 
Posts: n/a
Default Re: Segmentation Fault

In <jors6a$ind$1@speranza.aioe.org> Edward Rutherford <edward.p.rutherford79@REMOVETHIS.gmail.com> writes:

> It couldn't hurt but it wouldn't help either, since stdout gets flushed
> automatically at a new-line!


Only if the output is going to a terminal. If it's going to a file,
the output could get buffered and then lost upon segfault.

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Reply With Quote
  #10 (permalink)  
Old 05-14-2012, 09:17 PM
BartC
Guest
 
Posts: n/a
Default Re: Segmentation Fault

"pembed2012" <once@was.enough.invalid> wrote in message
news:jorcqt$72h$1@speranza.aioe.org...

> void
> srtn (int atm[10], int ctm[10])
> {
> int rng = 0;
> int qu[15];


> qu[size] = j;
> qu[size + 1] = -1;


'size' gets to 16 at this point; the upper bound of qu is 14.

At least, when called with both atm[] and ctm[] set to all zeros. Does it
depend on these having sensible data for it to work? In that case you might
post some expected input.

--
Bartc



Reply With Quote
  #11 (permalink)  
Old 05-14-2012, 09:21 PM
James Kuyper
Guest
 
Posts: n/a
Default Re: Segmentation Fault

On 05/14/2012 05:00 PM, pembed2012 wrote:
> On Mon, 14 May 2012 13:25:58 -0400, James Kuyper wrote:

....
>> it could be very difficult to locate the problem. It would make things a
>> lot easier for those interested in helping you, if you could provide a
>> complete, simple program demonstrating the problem. That program should
>> set atm and ctm to point at appropriately initialized arrays. It would
>> also be helpful to know precisely which compiler you're using, and with
>> what options turned on.


> Greatful if anyone is able to do a full debugging of this function.


In order for anyone else to debug your code, you'll need to embed it in
a complete program, as I mentioned in the paragraph quoted above. That
program needs to set up the required initial values of the arrays that
are passed to srtn(). We don't know how they should be set. If you don't
know how those initial values should be set, either, then that's the
source of your problem, and no further explanation is needed.

> Basically it's a kind of queue finding waiting and turnaround times. I
> don't know exactly how it's working.


That makes it sound like it's someone else's code. If that's the case,
the best place to look for information is the source of the code. Can
you identify a person or an organization responsible for the code, and
ask your questions of them?

If it were your own code, "I don't know exactly how it's working" would
be, in itself, all of explanation that would be needed for why it isn't
working.

Reply With Quote
  #12 (permalink)  
Old 05-14-2012, 09:24 PM
John Gordon
Guest
 
Posts: n/a
Default Re: Segmentation Fault

In <jorrpp$hrp$1@speranza.aioe.org> pembed2012 <once@was.enough.invalid> writes:

> Greatful if anyone is able to do a full debugging of this function.
> Basically it's a kind of queue finding waiting and turnaround times. I
> don't know exactly how it's working.


I did some quick and dirty debugging, and after a few iterations of the
main loop, the segfault is triggered in this section of code:

for(j = i + 1; qu[j] != -1; j++)
wt[qu[j]]++;

j ends up with values greater than 14, which makes it run off the end of
the qu array and eventually you end up trying to access a random element
of wt, whiuch causes a segfault.

This is happening because there isn't a -1 in the qu array where you
expected there to be.

I don't know why *that* is happening, but I figured I'd leave something
for you to solve. :-)

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Reply With Quote
  #13 (permalink)  
Old 05-14-2012, 09:35 PM
John Gordon
Guest
 
Posts: n/a
Default Re: Segmentation Fault

In <jort5t$cuo$1@reader1.panix.com> John Gordon <gordon@panix.com> writes:

> I did some quick and dirty debugging, and after a few iterations of the
> main loop, the segfault is triggered in this section of code:


> for(j = i + 1; qu[j] != -1; j++)
> wt[qu[j]]++;


This is with the atm and ctm arguments set to all zeroes. However,
after reading BartC's reply, I tried it with those two arrays set to
all -1's and did not see a seg fault.

If the behavior of your code depends on certain input, it would be
very helpful to provide that input.

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Reply With Quote
  #14 (permalink)  
Old 05-15-2012, 07:27 PM
pembed2012
Guest
 
Posts: n/a
Default Re: Segmentation Fault

On Mon, 14 May 2012 21:24:14 +0000, John Gordon wrote:

> In <jorrpp$hrp$1@speranza.aioe.org> pembed2012 <once@was.enough.invalid>
> writes:
>
>> Greatful if anyone is able to do a full debugging of this function.
>> Basically it's a kind of queue finding waiting and turnaround times. I
>> don't know exactly how it's working.

>
> I did some quick and dirty debugging, and after a few iterations of the
> main loop, the segfault is triggered in this section of code:
>
> for(j = i + 1; qu[j] != -1; j++)
> wt[qu[j]]++;
>
> j ends up with values greater than 14, which makes it run off the end of
> the qu array and eventually you end up trying to access a random element
> of wt, whiuch causes a segfault.
>
> This is happening because there isn't a -1 in the qu array where you
> expected there to be.
>
> I don't know why *that* is happening, but I figured I'd leave something
> for you to solve. :-)


Thanks but I think I will need more help here with a full debugging.

The code was by a "genius programmer" who has gone away, his code was
superefficient but maybe the variable names were not helpful and the
documentation was minimal :-)

What conditions do I need on atm and ctm when they get passed into ensure
no segfaults? Basically the function should be doing a kind of job queue,
I think qu=queue, wt=wait, tarnd=turn-around, wt=wait etc.

Thanks
Reply With Quote
  #15 (permalink)  
Old 05-15-2012, 07:38 PM
BartC
Guest
 
Posts: n/a
Default Re: Segmentation Fault

"pembed2012" <once@was.enough.invalid> wrote in message
news:jouanj$jhq$1@speranza.aioe.org...


> The code was by a "genius programmer" who has gone away, his code was
> superefficient but maybe the variable names were not helpful and the
> documentation was minimal :-)
>
> What conditions do I need on atm and ctm when they get passed into ensure
> no segfaults? Basically the function should be doing a kind of job queue,
> I think qu=queue, wt=wait, tarnd=turn-around, wt=wait etc.


In that case just rewrite it. That will be more reliable in the long term.
And you can take the opportunity to add some comments.

But you will need a specification, or know how it fits into the bigger
picture. Those hard-coded array bounds are a bit worrying, and confusing too
with one set of arrays having a length of 11, and an upper bound of 10, and
another set having a length of 10, and an upper bound of 9...

--
Bartc

Reply With Quote
 
Reply

Thread Tools
Display Modes

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 Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 12:20 PM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.