LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-14-2008, 11:36 AM   #1
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
C getopt segfault


Don't know what more to say here other than it keeps segfaulting and I haven't yet been able to figure out why. It does so while trying to access the optarg which gdb says is at address 0.

gcc version 4.2.3 20080113 (prerelease) (Debian 4.2.2-6)

Code:
/* gcc -Wall -o foo foo.c
* ./foo -x s -f 1.0 -p 2.0 -r 3.0 -t 4.0
*/

#include <getopt.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

int main(int argc, char *argv[0])
{
  extern char *optarg;
  extern int optind;

  int opt,
      flag; 

  char *ctype = NULL;

  double fv, pv;
  
  float r, t;

  while ((opt = getopt(argc, argv, "x::f::p::r::t::")) != -1) {
    switch(opt) {
      case 'x':
	ctype = optarg;
	break;
      case 'f':
	fv = (double)atof(optarg);
	break;
      case 'p':
	pv = (double)atof(optarg);
	break;
      case 'r':
	r = (float)atof(optarg);
	break;
      case 't':
	t = (float)atof(optarg);
	break;
      default:
	abort();
    }
  }

  return 0;
}
 
Old 01-14-2008, 11:51 AM   #2
rupertwh
Member
 
Registered: Sep 2006
Location: Munich, Germany
Distribution: Debian / Ubuntu
Posts: 297

Rep: Reputation: 49
Quote:
Originally Posted by h/w View Post
Code:
int main(int argc, char *argv[0])
That '0' certainly doesn't belong in there. It's 'char *argv[]' or 'char **argv'.
That might already fix your problem. Haven't looked at the rest.
 
Old 01-14-2008, 12:00 PM   #3
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
Quote:
Originally Posted by rupertwh View Post
That '0' certainly doesn't belong in there. It's 'char *argv[]' or 'char **argv'.
That might already fix your problem. Haven't looked at the rest.
Indeed, missed it among all the debugging trials. It wasn't there earlier, and was the last change (an error) I made.
The problem remains.
 
Old 01-14-2008, 12:39 PM   #4
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
OK, got it.
It's due to
Code:
while ((opt = getopt(argc, argv, "x::f::p::r::t::")) != -1) {
This page here - http://www.gnu.org/software/libc/man...l#Using-Getopt
mentions how we can use :: for switches that might have a value.

However, my getopt manpage doesn't mention it. Replacing the :: with a single : takes out the segfaults.

Last edited by h/w; 01-14-2008 at 12:42 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
getopt availability Bostonian Programming 2 12-26-2004 10:37 AM
getopt in unix hari_s_82 Linux - Newbie 2 10-01-2004 04:19 AM
getopt(3) Berhanie Programming 8 06-15-2004 09:50 PM
Getopt() newbie... AMMullan Programming 6 02-11-2004 01:02 PM
Using getopt in cplus UltimaGuy Programming 3 08-28-2003 05:35 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration