为什么从getopt_long()中省略这一行会产生段错误?

时间:2016-02-22 08:15:30

标签: c gnu glibc getopt getopt-long

在使用gcc -Wall getopt.c -o options进行编译并运行一些示例后,它似乎乍一看似乎有效。故意绊倒它使它成为Segfault。

//straight from the man page
static struct option long_options[] = {
    {"add",     required_argument, 0,  0 },
    {"append",  no_argument,       0,  0 },
    {"delete",  required_argument, 0,  0 },
    {"verbose", no_argument,       0,  0 },
    {"create",  required_argument, 0, 'c'},
    {"file",    required_argument, 0,  0 },
    {0,         0,                 0,  0 } //<-- if i omit this line, it segfaults
};

当我省略那一行时,为什么会导致分段错误?

或者更确切地说,以不同的方式提问

为什么我必须将最后一组struct option array成员初始化为null?

2 个答案:

答案 0 :(得分:2)

易。因此,处理数组的代码知道它何时结束。它被称为哨兵。

答案 1 :(得分:1)

来自man

  

数组的最后一个元素必须用零填充。

struct option {
           const char *name;
           int         has_arg;
           int        *flag;
           int         val;
       };

 The meanings of the different fields are:

  name   is the name of the long option.

 has_arg
       is: no_argument (or 0) if the option does not take an
          argument; required_argument (or 1) if the option requires an
          argument; or optional_argument (or 2) if the option takes an
          optional argument.

  flag   specifies how results are returned for a long option.  If flag
          is NULL, then getopt_long() returns val.  (For example, the
          calling program may set val to the equivalent short option
          character.)  Otherwise, getopt_long() returns 0, and flag
          points to a variable which is set to val if the option is
          found, but left unchanged if the option is not found.

  val    is the value to return, or to load into the variable pointed
          to by flag.

  The last element of the array has to be filled with zeros.

  If longindex is not NULL, it points to a variable which is set to the
   index of the long option relative to longopts.