2

I have a C program below written on 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

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
4
  • 1
    -1: Have you run your program in a debugger to locate when the seg-fault occurs? Commented Jun 3, 2011 at 13:49
  • @Oil, Yes I did. I am adding the dbx report Commented Jun 3, 2011 at 13:51
  • Ok, that's more like it! -1 removed... Commented Jun 3, 2011 at 13:54
  • @Oil, Thanks. Any idea why I am getting segmentation fault Commented Jun 3, 2011 at 13:55

5 Answers 5

9

The problem is that pOutFile is NULL when you try and print it. Many OSes (libc) doesn't handle this and you're trying to get it to print a variable that doesn't have a value.

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. Specifically the :s come after the letter. It should be this:

while( (c = getopt(argc, argv, "a:d:i:l:u:o:")) != EOF)
Sign up to request clarification or add additional context in comments.

4 Comments

My question is why pOutFile doesnt have a value. I have tried it by adding -o
Updated. It's still not safe to call printf and assume your user has supplied a value, so I still recommend the value check above even if you fix the -o problem.
Thanks Wes, I got the answer. It was a silly mistake but made me wandering for almost an hour.Appreciate your help
Many many times it takes another pair of eyes to spot silly mistakes. One can get "too close" to one's code :-)
3

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.

1 Comment

Yes, I am getting seg fault at the line you have specified, but I am not getting why?
3

Missing : is the problem:

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

2 Comments

beat me to it. :) @Nathan, it tells getopt that the -o option takes an argument.
Could you please let me know what :o mean and why we need to put :o:
2

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

You simply forgot to give the -o option:

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

Comments

2

You didn't pass -o before "out.txt", so you are dereferencing a null pointer in pOutFile's printf. That's what I noticed on first glance.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.