Mayank Agrawal
Mayank Agrawal

Reputation: 23

Segmentation Fault in Ubuntu

I keep getting Segmentation Fault Error when provided with the following arguments in Ubuntu

gcc program.c
./a.out $gjdvc1A

In the following code

# include <stdio.h>
 int lcount(char str[])
 { int i=0;
     while(str[i]!='\0')
     {
         i++;
     }
     if (i<5||i>15)
        printf("0");
     else
        printf("1");
 }
main(int argc , char *argv[])

{


    lcount( argv[1]);


}

Can any body help me and tell that why is this happening

Upvotes: 1

Views: 176

Answers (4)

Box Box Box Box
Box Box Box Box

Reputation: 5250

Well, I don't know about this problem very well, but I think I got the answer.

Basically, argv[] is an array hat holds your command line parameters, argv[1] is the first one (other than the command representation itself in argv[0], of course).

Well, when executing the program, when you pass the argument $gjdvc1A, the shell is evaluating it as an empty variable because of the symbol $.

Also, your program has a mistake:

Change lcount( argv[1]) to lcount( argv[1]);

To pass an argument with $ in the front, just put a \ which escapes the character.

This program you have written is bad, and you should do error checking, and of course, safety checks on arguments.

To make this program be better, use int main and put a return 0 at the end of the main.

Upvotes: 0

tano
tano

Reputation: 896

First you have to make some fixes to compile the program.

# include <stdio.h>
int lcount(char str[]) {
  int i=0;
  while(str[i]!='\0')
  {
    i++;
  }
  if (i<5||i>15)
    printf("0");
  else
   printf("1");
}

int main(int argc , char *argv[])
{
  lcount( argv[1]);
}

Then, the problem is not in your code (even if it should be heavily improved!). The problem is that you are calling it with an empty argument because $gjdvc1A is evaluated as an empty variable by the shell, and since you do not have any safety check in your code it will result in a segmentation fault.

Solution

  • Wrap program arguments in single quotes if you don't want them to be evaluated by the shell
  • Always perform safety check on program arguments!

Upvotes: 0

taskinoor
taskinoor

Reputation: 46037

When you pass an argument started with $ symbol in bash, bash treats it as a variable and try to get the value of that variable before passing that to the program. So when you are running ./a.out $gjdvc1A bash actually looks for variable gjdvc1A and since there is no variable with that name it passes nothing as argv[1]. In other words this is equivalent to running ./a.out without any argument. So when you try to access str[0] inside lcount function, you are trying to access a garbage address and thus getting the segmentation fault.

To solve the issue you need to escape $ or use quote while calling to tell bash not to treat it as a variable.

./a.out \$gjdvc1A

or

./a.out '$gjdvc1A'

In addition to this issue there are other problems too:

  • You should use int as return type of main and call return 0; at end of main.
  • There is a missing semicolon at line lcount( argv[1]).

Upvotes: 0

Muhammad Usama
Muhammad Usama

Reputation: 3147

You are passing the value of an environment variable $gjdvc1A as an argument. And your program does not have any argument checking, it will always crash when the value for the environment variable is not set

Upvotes: 1

Related Questions