c - scanf 导致段错误 11

标签 c segmentation-fault

我的代码运行良好。然后我开始遇到段错误。我尝试刷新缓冲区,但没有成功。 scanf 正在使用一个名为 frame_sizeint 变量,我认为这就是问题所在。有人能帮我解决这个段错误吗?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <string.h>

int main() {
    char ref[30];      // array that holds reference string
    int frame_size;    // maximum number of frames is 8
    // a frame holds a number thats in the reference stream.  
    int optimal_fault = 0;
    int lru_fault = 0; // least recently used faults
    int mfu_fault = 0; // most frequently used faults
    int lfu_fault = 0; // least frequently used faults    
    int type = 0;      // initializing to an invalid type in the switch statement
    int pages = 0;     //counts how many times you've looped
    //int page = 0;    //this will be the pages array 
    printf(" Please enter reference string: ");

    //fflush(stdin);    
    fgets(ref, 30, stdin);

    printf(" Please enter the number of frames(no more than 8 allowed)\n");

    scanf("%i[^\n]", &frame_size);

    int len = strlen(ref);
    int result = 0;
    int page[len];
    int i = 0;
    int k = 0;

    while (ref[i] != '0') {
        //page[i] = ref[i] *10 + (ref[i] -'O');
        /* if (ref[i] == ' ') {
            i++;
        }*/
        if (ref[i] != ' ' && ref[i+1] != ' ') {
            page[k] = ref[i] - '0' + (ref[i+1] - '0');  
            printf("page: %d", page[k]);
            k++;
            i++;
        }
        //printf("This is the string and number of frames: %d %i ", page[2],frame_size);
    }
    int frame[frame_size];// initializing frame array
    for (int i = 0; i < frame_size; i++) {
        frame[i] = -1; //which means the element is empty
    }

    //optimal algorithm
    /* for (int i = 0; i < ref.size(); i++) {
        printf("%d",ref.size());
    } */
    return 0;
}

最佳答案

您的代码中存在多个问题:

scanf("%i[^\n]", &frame_size);不会读取该行的其余部分,直到包含换行符,使用 scanf 执行此操作,你应该写:

if (scanf("%i%*[^\n]%*1[\n]", &frame_size) != 1) {
    /* complain about invalid input */
    exit(1);
}

但是使用 fgets 就简单多了读取一行并用 sscanf 解析它,特别是因为 scanf 版本不会处理非数字初始输入,并且您不会测试其返回值。

您这里有一个拼写错误:

while (ref[i] != '0')

迭代字符串 ref 的内容,你应该停在 '\0'NUL'0' 不同的字节,字符0,其ASCII值为48。

此外,这里从以字符表示的页码到整数值的转换不正确:

    if (ref[i] != ' ' && ref[i+1] != ' ') {
        page[k] = ref[i] - '0' + (ref[i+1] - '0');  
        printf("page: %d", page[k]);
        k++;
        i++;
    }

你应该这样写:

for (i = 0; ref[i] != '\0'; i++) {
    if (isdigit((unsigned char)ref[i] && isdigit((unsigned char)ref[i+1])) {
        page[k] = (ref[i] - '0') * 10 + (ref[i+1] - '0');  
        printf("page: %d", page[k]);
        k++;
        i++;
    }
}

您尝试实现的实际语义是模糊的,您应该在函数之前将它们记录在注释中。

另请注意以下建议:

  • 不要在此处发布 TAB 缩进代码,它的格式会很糟糕。在代码中使用 TAB 实际上是一个坏主意,TAB 在不同的系统中以不同的方式扩展,并导致您的代码在各种条件下变得不可读,如此处,但也在电子邮件等中。我必须重新格式化您的代码来修复它.

  • 请勿使用 /* */ 注释代码块,使用#if 0#endif

关于c - scanf 导致段错误 11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33724762/

相关文章:

python - 为什么 PyGILState_Release(...) 在这种情况下会出现段错误?

c - movq 指令出现段错误?

c - 包含引用本地 C 库的本地 Perl 模块

c++ - 如何在没有 Openssl 或其他库的情况下加载 RSA key 对

c# - 托管语言如何确保没有段错误

C++ std::vector 条目在函数中为 NULL,但大小保持在零以上

c++ - 特定线程上的 gdb nostop SIGSEGV

c - 将文本添加到行尾

c++ - 尝试用 codelite 编译

c++ - 涉及STL排序算法的令人困惑的SegFault