简体   繁体   English

C 程序导致分段错误

[英]C program causing segmentation fault

I have a C program below written on UNIX.下面我有一个 C 程序写在 UNIX 上。 I am getting segmentation fault.我遇到分段错误。 I am not getting where I am missing something.我没有得到我遗漏的东西。 Can anyone please help.任何人都可以请帮忙。

#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;
}

Running command运行命令

-a test -d deimf -i input.txt -l log.txt -u bc@abc -o out.txt

Output Output

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

Dbx Report数据库报告

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

The problem is that pOutFile is NULL when you try and print it.问题是当您尝试打印 pOutFile 时,它是 NULL。 Many OSes (libc) doesn't handle this and you're trying to get it to print a variable that doesn't have a value.许多操作系统(libc)不处理这个问题,你试图让它打印一个没有值的变量。

Try this:尝试这个:

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

Added:添加:

pOutFile doesn't have a value even when you specified the -o switch because you didn't put a: after the o in the getopt call.即使您指定了 -o 开关,pOutFile 也没有值,因为您没有在 getopt 调用中的 o 之后放置 a:。 Specifically the:s come after the letter.具体来说:s 出现字母之后。 It should be this:应该是这样的:

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

It looks like you're segfaulting on this line:看起来你在这条线上有段错误:

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

Judging by your commandline, you're not using a -o switch, so pOutFile remains NULL, but you're trying to printf it.从你的命令行来看,你没有使用-o开关,所以pOutFile仍然是 NULL,但你正在尝试printf它。

Missing : is the problem:缺少:是问题所在:

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

"Running command -a test -d deimf -i input.txt -l log.txt -u bc@abc out.txt" “运行命令 -a test -d deimf -i input.txt -l log.txt -u bc@abc out.txt”

You simply forgot to give the -o option:您只是忘记提供 -o 选项:

Running command -a test -d deimf -i input.txt -l log.txt -u bc@abc -o out.txt运行命令 -a test -d deimf -i input.txt -l log.txt -u bc@abc -o out.txt

You didn't pass -o before "out.txt", so you are dereferencing a null pointer in pOutFile's printf.您没有在“out.txt”之前传递 -o,因此您在 pOutFile 的 printf 中取消引用 null 指针。 That's what I noticed on first glance.这就是我第一眼注意到的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM