c - 为什么将我的输入分配给枚举会导致段错误?

标签 c memory segmentation-fault

我有以下程序

#include <stdio.h>
#include "dbg.h"

#define MAX_DATA 100

typedef enum EyeColor{
    BLUE_EYES, GREEN_EYES, BROWN_EYES, BLACK_EYES, OTHER_EYES
} EyeColor;

const char *EYE_COLOR_NAMES[] = {
    "Blue", "Green", "Brown", "Black", "Other"
};

typedef struct Person {
    int age;
    char first_name[MAX_DATA];
    char last_name[MAX_DATA];
    EyeColor eyes;
    float income;
} Person;

int main(int argc, char *argv[]){
    Person you = {.age = 0};
    int i = 0;
    char *in = NULL;

    printf("What's your First Name? ");
    in = fgets(you.first_name, MAX_DATA-1, stdin);  //fgets is much better
    check(in != NULL, "Failed to read first name.");

    printf("What's your Last Name? ");
    in = fgets(you.last_name, MAX_DATA-1, stdin);
    check(in != NULL, "Failed to read last name.");

    printf("How old are you? ");
    int rc = fscanf(stdin, "%d", &you.age);
    check(rc > 0, "You have to enter a number.");

    printf("What color are your eyes:\n");
    for(i = 0; i <= OTHER_EYES; i++) {
        printf("%d) %s\n", i+1, EYE_COLOR_NAMES[i]);
    }
    printf("> ");

    int eyes = -1;
    rc = fscanf(stdin, "%d", &eyes);
    check(rc > 0, "You have to enter a number.");

    you.eyes = eyes - 1;
    //check(you.eyes <= OTHER_EYES && you.eyes >= 0, "Do it right, that's not an option.");     // if you dont check this, seg fault can happen because of ...

    printf("How much do you make an hour? ");
    rc = fscanf(stdin, "%f", &you.income);
    check(rc > 0, "Enter a floating point number.");

    printf("------ RESULTS ------\n ");

    printf("First Name: %s", you.first_name);
    printf("Last Name: %s", you.last_name);
    printf("Age: %d\n", you.age);
    printf("Eyes: %s\n", EYE_COLOR_NAMES[you.eyes]);
    printf("Income: %f\n", you.income);

    return 0;
error:
    return 1;
}

如果我跑: $./ex24

我给出以下输入:

What's your First Name? a
What's your Last Name? b
How old are you? 1
What color are your eyes:
1) Blue
2) Green
3) Brown
4) Black
5) Other
> 1000
How much do you make an hour? 1
------ RESULTS ------
 First Name: a
Last Name: b
Age: 1
Segmentation fault (core dumped)

这个段错误是如何发生的? enum 不是在我的内存中分配 int 类型,应该可以容纳 1000 吗?

编辑1:将一些文本格式化为代码。 编辑2:注释掉检查。

最佳答案

枚举是否可以达到 1000 并不重要;您的数组没有 1000 个元素。
没有可打印的第 1000th 眼睛颜色。

这是您应该构建的测试用例:

#include <iostream>

const char* EYE_COLOR_NAMES[] = { "a", "b", "c", "d", "e" };
const int eyes = 1000;

int main()
{
    std::cout << EYE_COLOR_NAMES[eyes] << '\n';
}

我想说,很明显这是行不通的。

您已经在代码中对 you.eyes 进行了范围检查,并且您自己发现只有在进行范围检查时才会发生段错误。因此,只需保留范围检查即可。

关于c - 为什么将我的输入分配给枚举会导致段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27627333/

相关文章:

linux - 谁吃了我的内存?

c++ - 如何打印出C中变量的内存内容?

c - 如果我在 C 中取消引用非法地址,我会得到带有有效 gdb 回溯的 SIGSEGV。但是如果我将它传递给 snprintf backtrace 就会被丢弃

c - 为什么可以使用具有错误签名的比较函数调用 `qsort` 并且编译没有警告

c - RegisterDeviceNotification 获取 WM_DEVICECHANGE

memory - 在现有的大量代码中使用 jemalloc

c++ - 为什么我的代码在本地计算机中提供 segfault/stackoverflow 并在在线编译器中正确运行?

c - 解析一个字符串并将其分配给不同的字符

c - C 编译器如何解析以下 C 语句?

c - C 中的空指针比较