简体   繁体   English

意外的段错误-我在做什么错

[英]Unexpected Segfault - What am I doing wrong

I've been trying to blow the cobwebs off my C programming skills, and I've been getting an error I can't seem to figure out. 我一直在尝试从我的C编程技能中删除蜘蛛网,但我遇到了似乎无法弄清的错误。 This program reads in a list of integers separated by newlines. 该程序读取由换行符分隔的整数列表。 This bit happens in read_integer_file... I have no issues going through the input there. 这一点发生在read_integer_file中...通过那里的输入,我没有任何问题。 It's when I pass the data back to main via out that I have the problem. 当我将数据通过传回主数据库时,我遇到了问题。

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

int read_integer_file(char* filename, int* out)
{
    FILE* file;
    file = fopen(filename, "r");
    /* check if the file open was successful */
    if(file == NULL)
    {
        return 0;
    }

    int num_lines = 0;

    /* first check how many lines there are in the file */
    while(!feof(file))
    {
        fscanf(file, "%i\n");
        num_lines++;
    }

    /* seek to the beginning of the file*/
    rewind(file);

    out = malloc(sizeof(int)*num_lines);

    if(out == NULL)
        return 0;

    int inp = 0;
    int i = 0;
    while(!feof(file))
    {
        fscanf(file, "%i\n", &inp);
        out[i] = inp;
        printf("%i\n", out[i]); /* <---- Prints fine here! */

        i++;
    }

    return num_lines;
}

int main(int argc, char** argv)
{
    if(argc < 2)
    {
        printf("Not enough arguments!");
        return -1;
    }

    /* get the input filename from the command line */
    char* array_filename = argv[1];

    int* numbers = NULL;
    int number_count = read_integer_file(array_filename, numbers);

    for(int i = 0; i < number_count; i++)
    {
        /* Segfault HERE */
        printf("%i\n", numbers[i]);
    }
}

You have not allocated any memory for numbers. 您尚未为数字分配任何内存。 Currently it is pointing to no where. 目前,它没有指向任何地方。 When it gets back to the calling function it is still pointed to nowhere. 当它返回到调用函数时,仍然指向无处。 Pass a pointer to a pointer to the function to allocate it within the function. 将指针传递给该函数的指针以在函数内分配它。

int read_integer_file(char* filename, int** out)
{
     ...
     *out = malloc(sizeof(int)*num_lines);
     ...

     int number_count = read_integer_file(array_filename, &numbers);

This is a version of your code working.. Keep in mind also that fscanf just skip the \\n the way you wrote it so it's like writing fscanf(file, "%d"); 这也是代码工作的一个版本。还要记住,fscanf只是跳过\\ n编写方式,因此就像编写fscanf(file,“%d”);一样。

And if you don't put a variable to handle what it reads the compiler may not see it but you'll probably get an error.. 而且,如果您不使用变量来处理其读取的内容,则编译器可能看不到它,但是您可能会得到一个错误。

So here is the code : 所以这是代码:

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

int read_integer_file(char* filename, int **out)
{
    FILE* file;
    file = fopen(filename, "r");
    /* check if the file open was successful */
    if(file == NULL)
    {
        return 0;
    }

    int num_lines = 0;
    int garbi;
    char garbc;

    /* first check how many lines there are in the file */
    while(!feof(file))
    {
        fscanf(file, "%d", &garbi);
        fscanf(file, "%c", &garbc);
        if (garbc=='\n') ++num_lines;
    }

    /* seek to the beginning of the file*/
    rewind(file);

    int *nbr = malloc(sizeof(int)*num_lines);

    if(nbr == NULL)
        return 0;

    int i = 0;
    while(!feof(file))
    {
        fscanf(file, "%d", &nbr[i++]);
        fscanf(file, "%c", &garbc);
    }
    *out=nbr;

    return num_lines;
}

int main(int argc, char** argv)
{
    if(argc < 2)
    {
        printf("Not enough arguments!");
        return -1;
    }

    /* get the input filename from the command line */
    char* array_filename = argv[1];

    int *numbers = NULL;
    int number_count = read_integer_file(array_filename, &numbers);

    int i;
    for(i = 0; i < number_count; ++i)
        printf("%d\n", numbers[i]);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM