逐渐重新分配内存会导致分段错误

时间:2020-07-15 10:32:28

标签: c segmentation-fault realloc

我正在尝试在另一个函数中将新值附加到字符串c数组中,但是当我尝试添加第四个条目时,我只能添加三个记录,依此类推,发生段错误,这告诉我我使用了错误的记忆。据我所知,我逐渐按需分配内存为 realloc( array,array_length * sizeof(char )),所以它应该可以,但事实并非如此。另外,当我尝试在main()函数中追加新记录时,它的预期效果很好,有10条记录。

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

int array_length = 0;

void add(char **array, char *new)
{
    array_length++;
    array = realloc(*array, array_length * sizeof(char*));
    array[array_length-1] = malloc(50 * sizeof(char*));
    strcpy(array[array_length-1], new);
}

int main() 
{
    char **array = malloc (sizeof (*array));

    // First way
    add(&array, "first str");    // fine
    add(&array, "second str");   // fine
    add(&array, "third str");    // fine
    //add(&array, "fourth str"); // Segmentation fault (core dumped)    
    
    // Second way: 10 records successfully appended to array
    // for (int i = 0; i < 10; i++)
    // {
    //     array_length++;
    //     array = realloc(array, array_length * sizeof(char*));
    //     array[array_length-1] = malloc(50 * sizeof(char*));
    //     strcpy(array[array_length-1], "str");
    // }

    for (int i = 0; i < array_length; i++)
        printf("%s\n", array[i]);
    
    for (int j = 0; j < array_length; j++)
        free(array[j]);
    free(array);

    return 0;
}

1 个答案:

答案 0 :(得分:1)

您需要在char*函数中区分char**char***add。也许一种好方法是命名参数pArray来跟踪正在发生的事情:

int array_length = 0;

void add(char *** pArray, const char * new_val)
{
    array_length++;

    // we are modifying the original 'array' variable, 
    // so we need to assign the result to *pArray
    // (also note the sizeof used here)
    *pArray = realloc(*pArray, array_length * sizeof(char**));

    // from now on, we don't need to mutate the original variable,
    // so dereference 'pArray' into 'array'
    char ** array = *pArray;
    array[array_length - 1] = malloc(50 * sizeof(char));
    strcpy_s(array[array_length - 1], 49, new_val);
}

如果要避免三星级问题,请考虑将数组包装到结构中。这也更好,因为array_length不应该是全局变量。

例如:

typedef struct
{
    char ** array;
    size_t array_length;
}
string_list;

void add(string_list * s, const char * new_val)
{
    s->array_length++;
    s->array = realloc(s->array, s->array_length * sizeof(char**));

    s->array[s->array_length - 1] = malloc(50 * sizeof(char));
    strcpy_s(s->array[s->array_length - 1], 49, new_val);
}

void string_list_init(string_list * s)
{
    s->array = NULL;
    s->array_length = 0;
}

然后简单地:

string_list s;
string_list_init(&s);

// First way
add(&s, "first str");    // fine
add(&s, "second str");   // fine
add(&s, "third str");    // fine
add(&s, "fourth str"); // Segmentation fault (core dumped)