Segmentation Fault

I

iwasinnihon

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 atime[10], int ctime[10])
{
int running = 0;
int queue[15];
int wait[11];
int turnaround[11];
int i, j;
printf("This is a test");
for(i = 0; i < 11; i++)
{
wait = 0;
turnaround = 0;
}
queue[0] = 0;
queue[1] = -1;
int size = 1;
i = 0;
while(queue != -1)
{
ctime[queue]--;
running++;
for(j = i+1; queue[j] != -1; j++)
wait[queue[j]]++;
for(j = 0; j < 10; j++)
if(atime[j] != -1)
if(atime[j] <= running)
{
queue[size] = j;
queue[size+1] = -1;
size++;
}
turnaround[queue]++;
if(ctime[queue] == 0);
atime[queue] = -1;
for(j = 0; j < size; j++)
if(atime[queue[j]] != -1)
if(ctime[queue[j]] < ctime[queue])
i = j;
}
for(i = 0; i < 10; i++)
turnaround += wait;
for(i = 0; i < 10; i++)
{
wait[10] += wait;
turnaround[10] += turnaround;
}
wait[10] = wait[10] / 10;
turnaround[10] = turnaround[10] / 10;

print_report(wait, turnaround);
}
 
E

Eric Sosman

iwasinnihon said:
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?

I lack the patience and imagination to debug a function
whose purpose is not stated and whose call is not shown.
However,
void srtn(int atime[10], int ctime[10])
{
int running = 0;
int queue[15];
int wait[11];
int turnaround[11];
int i, j;
printf("This is a test");

.... Question 12.4 in the comp.lang.c Frequently Asked Questions
(FAQ) list at http://c-faq.com/ may change your view about how
far the function progresses before crashing.
 
C

CBFalconer

iwasinnihon said:
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?

Yes. It is lacking a 'main', and all the called routines are
undefined. It won't compile.
 
B

Bill Pursell

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?

Didn't spend much time looking at it, but it only takes
a minute to discover that your segfault occurs
at the 6th line of the while loop:
if(atime[j] != -1)

Learn to use a debugger: here's the relevant output from
gdb:

[tmp]$ gdb -q a.out core.27227
Using host libthread_db library "/lib/tls/libthread_db.so.1".
Core was generated by `./a.out'.
Program terminated with signal 11, Segmentation fault.
Reading symbols from /lib/tls/libc.so.6...done.
Loaded symbols for /lib/tls/libc.so.6
Reading symbols from /lib/ld-linux.so.2...done.
Loaded symbols for /lib/ld-linux.so.2
#0 0x08048470 in sortn (atime=0xffffffff, ctime=0xbfe80460) at a.c:28
28 if(atime[j] != -1)
 
J

jaysome

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?

Add:

#include said:
void srtn(int atime[10], int ctime[10])
{
int running = 0;
int queue[15];
int wait[11];
int turnaround[11];
int i, j;
printf("This is a test");

Others have noted the problem with this--a missing '\n'. In other
words, it should be:

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

It's a good idea to make sure this output is flushed, e.g.:

printf("This is a test\n");
fflush(stdout);
for(i = 0; i < 11; i++)

You use the magic number "11" here and above in the declaration for
wait. It's best to define a macro for this magic number, e.g.:

#define MAX_WAIT 11

and then use it as follows:

int wait[MAX_WAIT];

/* ... */

for(i = 0; i < MAX_WAIT; i++)
{
wait = 0;
turnaround = 0;
}
queue[0] = 0;
queue[1] = -1;
int size = 1;
i = 0;
while(queue != -1)
{
ctime[queue]--;
running++;
for(j = i+1; queue[j] != -1; j++)
wait[queue[j]]++;
for(j = 0; j < 10; j++)
if(atime[j] != -1)
if(atime[j] <= running)
{
queue[size] = j;
queue[size+1] = -1;
size++;
}
turnaround[queue]++;
if(ctime[queue] == 0);

^
PC-lint says:

Info 721: Suspicious use of ;

You don't want that semicolon.

After you fix that problem--and after you fix other problems like
including all the header files you need, like <stdio.h> for
printf--try running your code again and see if there's a problem. If
there still is, and you want help from this newsgroup, you're much
better off posting a complete example (including main) that compiles
and links and reproduces your problem.

Best regards
 
A

Army1987

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

It's a good idea to make sure this output is flushed, e.g.:

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

Isn't it automatically flushed whenever a newline character is printed?
 
J

Jens Thoms Toerring

Army1987 said:
"jaysome" <[email protected]> ha scritto nel messaggio
Isn't it automatically flushed whenever a newline character is printed?

Yes, if printf() printd to a terminal. But if stdout is redirected
to a file (or a pipe etc.) output is block buffered. Moreover, the
OP didn't even have a '\n' at the end of the string.

Regards, Jens
 
E

Eric Sosman

Richard said:
Army1987 said:


Yes.

"Yes" is a little too definite, I think. "Probably"
would be a more suitable answer. (Probably.)

(The issue hinges on whether the standard output is being
sent to an "interactive device," but each implementation has
its own notion of what "interactive" is. Best bet, IMHO, is
to send such messages to stderr instead of stdout. But if they
must to to stdout, make sure they end with '\n' or else call
fflush(), or for "belt *and* suspenders" do both as jaysome
suggests.)
 
K

Keith Thompson

Eric Sosman said:
"Yes" is a little too definite, I think. "Probably"
would be a more suitable answer. (Probably.)

(The issue hinges on whether the standard output is being
sent to an "interactive device," but each implementation has
its own notion of what "interactive" is. Best bet, IMHO, is
to send such messages to stderr instead of stdout. But if they
must to to stdout, make sure they end with '\n' or else call
fflush(), or for "belt *and* suspenders" do both as jaysome
suggests.)

stderr, as the name implies, should be used for error messages and
other run-time diagnostics. Writing a non-diagnostic message to
stderr just to make sure it's flushed is not a good idea; if you're
concerned about buffering, use fflush(). (In this case, it's not
clear whether "This is a test" should be considered diagnostic
output.)
 
A

Army1987

Keith Thompson said:
stderr, as the name implies, should be used for error messages and
other run-time diagnostics. Writing a non-diagnostic message to
stderr just to make sure it's flushed is not a good idea; if you're
concerned about buffering, use fflush(). (In this case, it's not
clear whether "This is a test" should be considered diagnostic
output.)

Well, there might be other uses. Imagine this:

#include <stdio.h>
/* definitions, inclusions etc. */

void get_a(long double array[], int length);
void get_b(long double array[], int length);
void frobnicate(long double a[], long double b[], int length,
long iteration);

int main(void)
{
long double a[LENGTH], b[LENGTH];
int i; long iL;
get_a(a, LENGTH);
get_b(b, LENGTH);
for (iL = 0; iL < INSANELY_LARGE_NUMBER; i++) {
printf("Please wait... %3d%% done. ",
(int)(iL * 100 / INSANELY_LARGE_NUMBER));
fflush(stdout);
frobnicate(a, b, LENGTH, iL);
putchar('\r');
}
puts("Results:");
for (i = 0; i < LENGTH; i++)
printf("%5d\t%Lg\t%Lg\t\n", i, a, b);
return 0;
}

What if output is redirected? It'd make more sense to write to stderr rather
than to stdout within the first for loop (altough it would not be completely
canonical, as some would disagree calling such output "diagnostic").
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top