-
February 11th, 2010, 04:41 PM
#1
Segmentation Fault (core dumped)
Hello All,
First post, please be gentle.
I'm trying to explore some code from the GNU website, however, I'm running into issues. I'm running a ubuntu core and when I try to run the code below (compiles without errors), it gives me a "Segmentation Fault (core dumped)" error. I understand that this error message deals with illegal usage of memory, however, I have not been able to fix the issue.
When running from console:
mason> speller
Segmentation Fault (core dumped)
mason> speller 5
Segmentation Fault (core dumped)
mason> speller -a 1
Segmentation Fault (core dumped)
mason> speller -c
Option -c requires an argument.
mason> speller -c 4
aflag = 0, bflag = 0, cvalue = 4
When typed simply as "speller", the program should just print out:
aflag = 0, bflag = 0, cvalue = (null)
Here is the code:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main (int argc, char **argv)
{
int aflag = 0;
int bflag = 0;
char *cvalue = NULL;
int index;
int c;
opterr = 0;
while ((c = getopt (argc, argv, "abc:")) != -1)
switch (c)
{
case 'a':
aflag = 1;
break;
case 'b':
bflag = 1;
break;
case 'c':
cvalue = optarg;
break;
case '?':
if (optopt == 'c')
fprintf (stderr, "Option -%c requires an argument.\n", optopt);
else if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n", optopt);
return 1;
default:
abort ();
}
printf ("aflag = %d, bflag = %d, cvalue = %s\n",
aflag, bflag, cvalue);
for (index = optind; index < argc; index++)
printf ("Non-option argument %s\n", argv[index]);
return 0;
}
Please let me know if anybody can help, it would be GREATLY appreciated.
Thanks
-
February 11th, 2010, 05:09 PM
#2
Re: Segmentation Fault (core dumped)
I'm not really familiar with getopt (I usually use Boost.Program Options instead), but I don't see anything there which is potentially problematic. Why don't you run the program in gdb to see where the offending line of code is? (You'll need to make sure you compile unoptimized and with debug symbols enabled.)
-
February 11th, 2010, 09:33 PM
#3
Re: Segmentation Fault (core dumped)
I am a little elementary with gdb, but I gave it a try. Below are my results:
mason> make
gcc -g -Wall -o speller speller.c
mason> gdb speller
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "sparc-sun-solaris2.10"...
(gdb) run
Starting program: /home/u2/akoros/ISA563/hw2/speller
Program received signal SIGSEGV, Segmentation fault.
0xff232150 in strlen () from /usr/lib/libc.so.1
(gdb) break main
Breakpoint 1 at 0x10824: file speller.c, line 8.
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/u2/akoros/ISA563/hw2/speller
[New LWP 1 ]
Breakpoint 1, main (argc=1, argv=0xffbffb54) at speller.c:8
8 int aflag = 0;
(gdb) step
9 int bflag = 0;
(gdb) print L
$1 = 0
(gdb) s
10 char *cvalue = NULL;
(gdb) s
14 opterr = 0;
(gdb) s
16 while ((c = getopt (argc, argv, "abc:")) != -1)
(gdb) list
11 int index;
12 int c;
13
14 opterr = 0;
15
16 while ((c = getopt (argc, argv, "abc:")) != -1)
17 switch (c)
18 {
19 case 'a':
20 aflag = 1;
(gdb) continue
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0xff232150 in strlen () from /usr/lib/libc.so.1
(gdb)
Is there something else that I should try within gdb?
-
February 11th, 2010, 09:45 PM
#4
Re: Segmentation Fault (core dumped)
I have also run a debug with the dev-c++ program that (i believe) uses the gdb..
here are the results after running the program:
Starting program: ........speller.exe -c
Program exited with error..
frames-invalid
frames-invalid
frames-invalid
frames-invalid
frames-invalid
starting
frames-invalid
exited 1
Program exited with code 01.
frames-invalid
stopped
pre-prompt
(gdb)
prompt
any suggestions?
-
February 11th, 2010, 09:48 PM
#5
Re: Segmentation Fault (core dumped)
Here is the gdb output from unix:
mason> make
gcc -g -Wall -o speller speller.c
mason> gdb speller
GNU gdb 6.8
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "sparc-sun-solaris2.10"...
(gdb) run
Starting program: /home/u2/akoros/ISA563/hw2/speller
Program received signal SIGSEGV, Segmentation fault.
0xff232150 in strlen () from /usr/lib/libc.so.1
(gdb) break main
Breakpoint 1 at 0x10824: file speller.c, line 8.
(gdb) run
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/u2/akoros/ISA563/hw2/speller
[New LWP 1 ]
Breakpoint 1, main (argc=1, argv=0xffbffb54) at speller.c:8
8 int aflag = 0;
(gdb) step
9 int bflag = 0;
(gdb) print L
$1 = 0
(gdb) s
10 char *cvalue = NULL;
(gdb) s
14 opterr = 0;
(gdb) s
16 while ((c = getopt (argc, argv, "abc:")) != -1)
(gdb) list
11 int index;
12 int c;
13
14 opterr = 0;
15
16 while ((c = getopt (argc, argv, "abc:")) != -1)
17 switch (c)
18 {
19 case 'a':
20 aflag = 1;
(gdb) continue
Continuing.
Program received signal SIGSEGV, Segmentation fault.
0xff232150 in strlen () from /usr/lib/libc.so.1
(gdb) quit
Is there anything else I should be doing?
-
February 11th, 2010, 10:14 PM
#6
Re: Segmentation Fault (core dumped)
Oh, you're at George Mason? That's where I got my Masters. Finished last summer.
Since gdb is identifying strlen() as the problem, and you clearly aren't calling that, it seems that it's probably an issue within the getopt() library. That doesn't mean the library is broken---there may just be an issue with its compile settings or something.
To get more information, gdb it again and this time get the backtrace ("bt") when it segfaults. You may also want to try running the program through valgrind.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|