Solved

Why am I getting a seg fault?

Posted on 2015-02-04
7
58 Views
Last Modified: 2015-05-20
Here's the code

int main(int argc, char* argv[1])
{
char charOld = argv[4][0];
char charNew = argv[5][0];
}
~

Open in new window


I know I'm not seeing something simple. What am I over looking in terms of the seg fault??
0
Comment
Question by:cgunix
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 13

Accepted Solution

by:
Mark Bullock earned 250 total points
ID: 40590398
argv is an array of char*, which point to strings.
argv[0] contains a pointer to the first string, the name or pathname of the program.
argv[0][1] contains the second character of that string.
So swap the indexes in your code, e.g. argv[0][4]
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40590400
Why aren't you checking the value of argc? How would you know how many arguments were passed to your program?
0
 
LVL 13

Expert Comment

by:Mark Bullock
ID: 40590412
argc is always >= 1 the first argument is the name or pathname of the program.
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 250 total points
ID: 40590436
If you don't pass at least 5 command line arguments to  your program, argv[5] will be undefined.

If the program name is not at least 5 characters long, argv[0][5] will be undefined.
0
 
LVL 34

Expert Comment

by:sarabande
ID: 40591617
I think Mark is right and you mixed up the array indices. the first index counts the pointers to char, the second index counts the characters in the argument string.

If you don't pass at least 5 command line arguments to  your program, argv[5] will be undefined.
it is not only undefined but argv[5] is an invalid pointer value - probably 0 - what causes the segmentation fault. when accessing the argv array elements you always should follow the advice kaufmed has given and check for the argc argument before accessing argv elements:

if (argc > 1)
{
       // now it is safe to use the argv[1] argument
       int len = strlen(argv[1];
       ....
}
if (argc > 2)
{
       // now it is safe to use the argv[2] argument
       std::cout << argv[2] << std::endl;
       ...
}
....

Open in new window


or

if (argc <= 3)
{
       std::cout << "program requires at least 3 arguments" << std::endl;
       return -1;
}
// here it is safe to access argv[1], argv[2], or argv[3]

Open in new window


Sara
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question