无法找出段错误

标签 c segmentation-fault switch-statement getopt getopt-long

当我尝试在命令行中传递 -H 作为标志时,为什么总是出现设置错误? -h(帮助)工作正常,但 -H(标题)每次都会搞砸。

我还有一个 main 函数,它通过传递 argc 和 argc 来调用 parse_command_line。

bool 定义为 bool header = false;

文件是 char** file = NULL;

以及我拥有该文件+=1的原因;代码中的内容是这样的,因为我使用的 makefile 将所有警告更改为错误,因此它可以编译。

#include "parse.h"              /* prototypes for exported functions */
#include "../main/unused.h"
#include <stdlib.h>
#include <getopt.h>
#include <stdio.h>
#include <stdint.h>

int
parse_command_line (int argc, char **argv, bool *header, char **file)
{
int oc = 0;
file += 1;
bool help = false;
struct option long_options[] = 
{
    {"header", no_argument, NULL, 'H'},
    {"help", no_argument, NULL, 'h'},
    {0, 0, 0, 0}
};

while ((oc = getopt_long(argc, argv, "+Hh", long_options, NULL)) != -1)
{
    printf("The value of oc = %d\n", oc);

    switch(oc)
    {
        case 'h':
            help = true;
            break;
        case 'H':
            printf("inside case H");
            *header = true;
            break;
        case '?':
            fprintf(stderr, "Unknown flag = -%c, type -h or --help for help!\n", optopt);
            exit(EXIT_FAILURE);
            break;
        default:
            break;
    }
}

printf("Out of loop");    if (optind+1 != argc)
{
    fprintf(stderr, "Uh oh, invalid input! Try again with -h or --help for help!\n");
    exit(EXIT_FAILURE);
}

if (help)
{
    printf("\nHaving some trouble? Let me show you the ropes!\n\n");
    printf("Format: ydi <option(s)> mini-elf-file\n\n");
    printf("Here's your options:\n");
    printf("-h --help       Display usage\n");
    printf("-H --header     Show the Mini-Elf header\n");
    exit(1);
}

if (header)
{
    printf("Inside HEader");
    FILE *file;
    uint16_t nums[6];
    file = fopen(argv[optind], "r");

    #define STRUCT_ITEMS 7
    fread(nums, 16, 6, file);
    int cur_print;
    for (cur_print = 0; cur_print < STRUCT_ITEMS; cur_print++)
    {
        printf("%d ", nums[cur_print]);
    }
}
return 0;
} 

我的parse.h文件如下:

#ifndef __PARSE_COMMAND_LINE__
#define __PARSE_COMMAND_LINE__
#include <stdbool.h>

int parse_command_line (int argc, char **argv, bool *header, char **file);

#endif

还有其他文件,例如 elf.h 和 elf.c,我尚未实现,并且此时根本没有调用它们,这让我相信它们不会成为问题,也不需要发布小 2 行文件。我的主要功能如下:

#include <stdio.h>              /* standard I/O */
#include <stdlib.h>
#include "unused.h"             /* UNUSED macro */
#include "../cmdline/parse.h"   /* command line parser */
#include "../y86/elf.h"         /* Mini-ELF format */

int
main (int argc UNUSED, char **argv UNUSED)
{
  printf ("Congratulations, you have compiled your source code!\n");
  bool header = false;
  char **file = NULL;
  parse_command_line (argc, argv, &header, file);
  return 0;
}

文件used.h(因为编译器会让未使用的变量成为错误而不是警告)如下:

#ifndef __UNUSED__
#define __UNUSED__
#define UNUSED __attribute__ ((unused))
#endif

最佳答案

该代码不检查 fopen 的返回值,如果出现错误,该返回值将为 NULL。在 fread 调用中取消引用 NULL 会导致段错误。

关于无法找出段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35097430/

相关文章:

c++ - 分段故障

java - 嵌套 Switch 语句中缺少 Return 语句

java - 循环 Switch 案例

c++ - 位移位和算术运算有什么区别?

c - 错误(段错误核心转储)-列表和结构指针

c - CC/GCC 但不是 G++ (C/SDL2/Linux) 的奇怪段错误

Java switch 语句

c - 返回一个文件数组

CS50 PSet 2 : Vigenere cipher Segmentation Fault

c - 在 C 中处理多字节(非 ASCII)字符