2011-06-03 74 views
2

我有一个C程序写在UNIX上。我正在分段错误。我没有得到我错过的东西。任何人都可以请帮忙。C程序导致段错误

#include <stdio.h> 
#include <stdlib.h> 
#include <strings.h> 
char*     app_name = NULL; 
char*   pInFile = NULL; 
int main(int argc, char *argv[]) 
{ 
    char*    arg0 = argv[0]; 
    char*    pdebug = "miecf"; 
    char*      pLogfile = NULL; 
    char*    pUserid = NULL; 
    char*    pOutFile = NULL; 
    int   c; 

    while((c = getopt(argc, argv, ":a:d:i:l:u:o")) != EOF) 
    { 
     switch (c) 
     { 
     case 'a': 
      app_name = optarg; 
      break; 

     case 'd': 
      pdebug = optarg; 
      break; 

     case 'i': 
      pInFile = optarg; 
      break; 

     case 'l': 
      pLogfile = optarg; 
      break; 

     case 'u': 
      pUserid = optarg; 
      break; 

     case 'o': 
      pOutFile = optarg; 
      break; 

     default: 
       fprintf(stderr, "unknown option \'%c\'\n", optopt); 
       break; 
     } /* switch(c) */ 
    } /* while(getopt()) */ 


     printf("app_name is [%s]\n",app_name); 
     printf("pdebug is [%s]\n",pdebug); 
     printf("pInFile is [%s]\n",pInFile); 
     printf("pLogfile is [%s]\n",pLogfile); 
     printf("pUserid is [%s]\n",pUserid); 
     printf("pOutFile is [%s]\n",pOutFile); 

     return 0; 
} 

运行命令

-a test -d deimf -i input.txt -l log.txt -u [email protected] -o out.txt 

输出

app_name is [test] 
pdebug is [deimf] 
pInFile is [input.txt] 
pLogfile is [log.txt] 
pUserid is [[email protected]] 
run[2]: 10448 Segmentation Fault(coredump) 

DBX报告

program terminated by signal SEGV (no mapping at the fault address) 
0xff232370: strlen+0x0050:  ld  [%o2], %o1 
(dbx) where 
=>[1] strlen(0x0, 0xfffffaf0, 0x0, 0xffbff1a8, 0x0, 0x2b), at 0xff232370 
    [2] _ndoprnt(0x10f77, 0xffbff26c, 0xffbfe8e9, 0x0, 0x0, 0x0), at 0xff29e4d4 
    [3] printf(0x10f68, 0x21100, 0x0, 0x2111e, 0xff3303d8, 0x14), at 0xff2a0680 
    [4] main(0xc, 0xffbff304, 0xffbff4ad, 0xffbff4b8, 0x0, 0xffffffff), at 0x10e8 
+1

-1:你在调试器中运行程序时,赛格发生故障定位? – 2011-06-03 13:49:21

+0

@油,是的,我做过。我正在添加dbx报告 – 2011-06-03 13:51:29

+0

好吧,那更像是它! -1删除... – 2011-06-03 13:54:29

回答

9

问题是当您尝试打印它时,pOutFile为NULL。许多操作系统(libc)不处理这个问题,而你试图让它打印一个没有值的变量。

试试这个:

if (pOutFile != NULL) 
    printf("pOutFile is [%s]\n",pOutFile); 
else 
    printf("pOutFile is NULL\n"); 

补充:当您指定-o开关

pOutFile没有价值,甚至因为你没有把一:后邻在getopt调用中。具体来说:在之后来信。它应该是这样的:

while((c = getopt(argc, argv, "a:d:i:l:u:o:")) != EOF) 
+0

我的问题是为什么pOutFile没有价值。我已通过添加-o – 2011-06-03 13:55:12

+2

更新过。调用printf并且*假设*你的用户提供了一个值仍然不安全,所以即使你修复了-o问题,我仍然推荐上面的值检查。 – 2011-06-03 13:59:04

+0

谢谢Wes,我得到了答案。这是一个愚蠢的错误,但让我徘徊了近一个小时。感谢您的帮助 – 2011-06-03 14:03:56

3

看起来你在这一行段错误:

printf("pOutFile is [%s]\n",pOutFile); 

,你不使用-o开关通过命令行的情况来看,这样pOutFile保持为空,但你要printf它。

+0

是的,我在你指定的行得到seg故障,但我不明白为什么? – 2011-06-03 14:01:56

2

“运行命令-a测试-d deimf -i input.txt中的log.txt -l -u BC @ ABC out.txt”

你根本忘了给-o选项:

运行命令-a测试-d deimf -i input.txt中的log.txt -l -u BC @ ABC -o out.txt

+0

我已经加了-o,仍然没有解决问题 – 2011-06-03 13:54:27

2

您没有通过-o前“out.txt”,所以你在pOutFile的printf中取消了一个空指针。这就是我第一眼看到的。

3

缺少:的问题是:

while((c = getopt(argc, argv, ":a:d:i:l:u:o:")) != EOF) 
              ^
+0

这是干什么的? – 2011-06-03 14:01:26

+0

击败了我。 :) @Nathan,它告诉'getopt' -o选项带有一个参数。 – andrewdski 2011-06-03 14:04:07

+0

谢谢Codaddict – 2011-06-03 14:04:26