seg fault

B

Bill Cunningham

This program work fine except if it is run with argc==0 it seg faults. I
thought about using switch but I'm not the familiar with it so stick to the
if and else if stuff.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
if (argc > 4 || argc == 0) {
fputs("dicegenerator usage error\n", stderr);
exit(EXIT_FAILURE);
}

int x;
x = atoi(argv[1]);
srand(time(NULL));
printf("%i\n", rand() % x);
return 0;
}

Bill
 
K

Keith Thompson

Bill Cunningham said:
This program work fine except if it is run with argc==0 it seg faults. I
thought about using switch but I'm not the familiar with it so stick to the
if and else if stuff.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
if (argc > 4 || argc == 0) {
fputs("dicegenerator usage error\n", stderr);
exit(EXIT_FAILURE);
}

int x;
x = atoi(argv[1]);
srand(time(NULL));
printf("%i\n", rand() % x);
return 0;
}

No, it doesn't seg fault with argc==0.

What makes you think argc==0?

I think you're assuming that running the program with no arguments
causes argc to be 0. You're wrong. (Why did you get this wrong?
Because you persist in guessing.)

Add this line:

printf("argc = %d\n", argc);

after the opening "{" of your main function and try it again.
It won't fix your problem, but it should help you understand just
what the problem is.

Question: What does the value of argc actually mean? Don't answer
here; we already know. Look up "argc" in the index of K&R2 and
read the section it points to. (I verified this time that the
information is actually there.)

It looks like you've given up on using strtol(). Apparently the
time several of us spent trying to encourage you to figure it out
were wasted -- unless you're just temporarily falling back to the
simpler atoi() for now, and intend to try strtol() again later.
But historically, every time you've run into difficulties,
you've eventually given up and gone off on some other tangent.
You'll never get anywhere that way. (My use of the word "never"
is *not* intended as an exaggeration.)
 
I

Ike Naar

This program work fine except if it is run with argc==0 it seg faults. I
thought about using switch but I'm not the familiar with it so stick to the
if and else if stuff.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(int argc, char *argv[])
{
if (argc > 4 || argc == 0) {
fputs("dicegenerator usage error\n", stderr);
exit(EXIT_FAILURE);
}

int x;
x = atoi(argv[1]);
srand(time(NULL));
printf("%i\n", rand() % x);
return 0;
}

When you run the program without any arguments, you probably think that
in that case arg==0, but it's not.
Traditionally, argv[0] contains the name of the program.

So, if you run the program without any arguments on the command line,
then argc==1 and argv[0] contains the program name.
If you run the program with, say, one argument on the command line,
then argc==2, argv[0] contains the program name, and argv[1] contains the
argument you provided on the command line.

It might be instructive to print the value of argc at the start of main().
 
B

Beej Jorgensen

Bill Cunningham said:
This program work fine except if it is run with argc==0 it seg faults.
I thought about using switch but I'm not the familiar with it so stick
to the if and else if stuff.

This is one of your better programs, Bill, honestly, and it's very close
to working.

Question number one for segfaults is: what line does it segfault on?

-Beej
 
B

Bill Cunningham

When you run the program without any arguments, you probably think that
in that case arg==0, but it's not.
Traditionally, argv[0] contains the name of the program.

So, if you run the program without any arguments on the command line,
then argc==1 and argv[0] contains the program name.
If you run the program with, say, one argument on the command line,
then argc==2, argv[0] contains the program name, and argv[1] contains the
argument you provided on the command line.

It might be instructive to print the value of argc at the start of main().

Oh ok I see.

Bill
 
B

Bill Cunningham

Beej Jorgensen said:
This is one of your better programs, Bill, honestly, and it's very close
to working.

Question number one for segfaults is: what line does it segfault on?
I am going to continue with this and see how far I cane get.

Bill
 
B

Barry Schwarz

This is one of your better programs, Bill, honestly, and it's very close
to working.

Given some of his abominations, it probably is closer to the top than
the bottom. He did a much superior job on indenting than normal. But
it hardly qualifies as close to working.

Why does a program that expects only one argument ask if argc is
greater than 4? What is magical about argc being 0? What happens
when argc is 1?

Aside from being factually correct, the diagnostic message gives the
user absolutely no useful information.

What happens when atoi returns 0?

In C89, declarations must be at the beginning of a block.
 
B

Beej Jorgensen

Bill Cunningham said:
I am going to continue with this and see how far I cane get.

For locating segfaults, you have a number of options.

* desk check it and see if there's anything obvious

* use a debugger to tell you where the crash happened

* try putting printf()s in the code in various places and see how many
of them print before it crashes

* try exit()ing from main() early--if it exits before the crash, you
know the crash is happening later in the code

-Beej
 
K

Keith Thompson

Beej Jorgensen said:
For locating segfaults, you have a number of options.

* desk check it and see if there's anything obvious

* use a debugger to tell you where the crash happened

* try putting printf()s in the code in various places and see how many
of them print before it crashes

Either print to stderr or call fflush(stdout) after each message. A
seg fault might kill the program before any buffered output has been
printed.
 
B

Bill Cunningham

Given some of his abominations, it probably is closer to the top than
the bottom. He did a much superior job on indenting than normal. But
it hardly qualifies as close to working.

Barry it does work. What I am asking for is if the program name is
called without any args it is to fail. If more than argv to the program it
is to fail. I know this can be done I've seen programs that do this. Now if
I know what I'm doing and how to go about doing this is another matter.

Bill
 

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

Similar Threads

C99 Seg fault on while(), why ? 0
seg fault 76
Command Line Arguments 0
seg fault 11
Linux: using "clone3" and "waitid" 0
pointer to int error 24
dice generator problems 43
code works 11

Members online

No members online now.

Forum statistics

Threads
473,880
Messages
2,569,944
Members
46,246
Latest member
RosalieMar

Latest Threads

Top