简体   繁体   English

对结构数组进行Realloc,在索引时解决边界错误

[英]Realloc on an array of structs, address boundary error when indexing

I have some code where I'm trying to read lines in from a file and store some information from each line in a struct. 我有一些代码,我试图从文件中读取行,并存储结构中每行的一些信息。 Since I don't know how long the file will be, I'm dynamically adjusting the array of structs using realloc . 由于我不知道文件有多长,我正在使用realloc动态调整结构数组。

My issue is that my code seems to work fine for the first 3 (technically 6) lines, and then I receive SIGSEGV (address boundary error). 我的问题是我的代码似乎适用于前3行(技术上为6行),然后我收到SIGSEGV(地址边界错误)。 gdb says that this happens when trying to index the array ( array[i]->string = (char*) _tmp ). gdb说当尝试索引数组时会发生这种情况( array[i]->string = (char*) _tmp )。

typedef struct {
    char* string;
    int len;
} buffer;


int read_into_array(char *filename, buffer** array) {
    int n;
    size_t size;
    char* buf = NULL;
    FILE *file = fopen(filename, "r");

    int i = 0;
    while (1) {
        buffer *tmp = (buffer*)realloc(*array, sizeof(buffer) * (i + 1));
        if (!tmp)
            printf("Failed realloc\n");

        *array = tmp;

        // First line is ignored, second line is taken as data.
        getline(&buf, &size, file);
        n = getline(&buf, &size, file);
        if (n > 0) {
            void* _tmp = malloc(sizeof(char) * n);
            if (!_tmp)
                printf("Failed malloc\n");

            array[i]->string = (char*) _tmp;
            array[i]->len = n-1;
            strncpy(array[i]->string, buf, n-1);
        }

        i++;
        if (feof(file)) {
            printf("saw end of file, leaving.\n");
            break;
        }
    }

    return i;
}

int main(int argc, char* argv[]) {
    char *filename = argv[1];

    buffer *array = (buffer*) calloc(1, sizeof(buffer));
    int num = read_into_array(filename, &array);
}

Apologies for the somewhat poor formatting, I've been trying to figure this out for a while. 对格式有点差的道歉,我一直试图弄清楚这一点。

Since it seems to work for the first few lines, my assumption is that I'm going wrong somewhere in the realloc calculation. 由于它似乎适用于前几行,我的假设是我在realloc计算中的某个地方出错了。 My other guess is that I'm somehow using/reading the file incorrectly. 我的另一个猜测是我以某种方式错误地使用/读取文件。

Thanks for any help. 谢谢你的帮助。 For posterity, the file looks something like this https://hastebin.com/vinidiyita.sm (the real file is thousands of lines long). 对于后代,该文件看起来像这样https://hastebin.com/vinidiyita.sm (真正的文件长达数千行)。

when you do *array=tmp you're allocating memory for array[0] 当你执行* array = tmp时,你要为数组[0]分配内存

then you're using array[i] that should be a pointer to a buffer, but points to garbage or 0 然后你使用的数组[i]应该是指向缓冲区的指针,但指向垃圾或0

You're confusing two ways to use data. 你混淆了两种使用数据的方法。

The first is by using arrays - there's the non-dynamic: 第一个是使用数组 - 这是非动态的:

buffer array[x] = {0};
int num = read_into_array(filename, &array);

then you can use array[i] 然后你可以使用数组[i]

and there's the dynamic type: 并且有动态类型:

buffer **array = calloc(initial_len*sizeof(buffer *));
int num = read_into_array(filename, array, initial_len);
read_into_array(char *filename, buffer **&array, int initial_len)
{
    int len = initial_len;
...
    while()
    {
        ...
        if(i>len)
        {
            array = realloc(array, sizeof(buffer*) * (i + 1));
            len = i;
        }
        array[i] = calloc(sizeof(buffer));
    }
}

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

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