C语言 重新分配结构数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6169972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:47:47  来源:igfitidea点击:

Realloc an array of Structs

carraysstructrealloc

提问by thelionroars1337

I am trying to dynamically reallocate memory for an array of structs (actually an array each of 2 structs but 1 included here for simplicity) that is being read from/to a file or inputted by the user.

我正在尝试为正在从/向文件读取或由用户输入的结构数组(实际上是 2 个结构中的每一个,但为了简单起见包括 1 个结构)动态重新分配内存。

typedef Struct
{
    char surname[21];
    char firstname[21];
    char username[21];
...
} User;

...in main():

...在主():

int size = 0; /* stores no. of structs */
User* user_array = (User *) calloc(1, sizeof(User));
if(user_array == NULL)
{
    printf("Cannot allocate initial memory for data\n");
    exit(1);
}
else
    size++;

I am then trying to use a function call to increase the array when needed:

然后我尝试在需要时使用函数调用来增加数组:

int growArray(User user_array*, int size)
{
    User *temp;
    size++;
    temp = (User *) realloc(user_array, (size * sizeof(User));
    if(temp == NULL)
    {
        printf("Cannot allocate more memory.\n");
        exit(1);
    }
    else
        user_array = temp;
    return size;
}

Unfortunately the realloc never works. Both structs are only about 200 bytes per instance and setting the initial size to say 10 will work fine, so there must be something wrong with the way I am trying to use realloc.

不幸的是,realloc 永远不会工作。这两个结构每个实例只有大约 200 个字节,将初始大小设置为 10 就可以了,所以我尝试使用 realloc 的方式一定有问题。

System is Win 7 64, on Core i5 with 4GB, running Quincy (a MinGW GUI).

系统是 Win 7 64,在 4GB 的 Core i5 上,运行 Quincy(一个 MinGW GUI)。

回答by Node

reallocchanges the size of the memory pointed to by user_arrayto the specified size, it doesn't increase it by size. Seeing as your function is called growArray, i'd presume you want it to increase the size of the array by size, in which case you need to:

realloc将 指向的内存大小更改为user_array指定的大小,它不会按大小增加它。看到你的函数被调用growArray,我假设你希望它增加数组的大小size,在这种情况下你需要:

int growArray(User **user_array, int currentSize, int numNewElems)
{
    const int totalSize = currentSize + numNewElems;
    User *temp = (User*)realloc(*user_array, (totalSize * sizeof(User)));

    if (temp == NULL)
    {
        printf("Cannot allocate more memory.\n");
        return 0;
    }
    else
    {
        *user_array = temp;
    }

    return totalSize;
}

Note that growArraytakes the address of user_array, the reason for this is that reallocmight move the memory if it couldn't extend the existing block to the required size.

请注意,growArray采用 的地址user_array,这样做的原因是,realloc如果无法将现有块扩展到所需的大小,则可能会移动内存。

To use it:

要使用它:

int size = 0;
User* user_array = (User *) calloc(1, sizeof(User));
if(user_array == NULL)
{
    printf("Cannot allocate initial memory for data\n");
    exit(1);
}

/* add 10 new elements to the array */
size = growArray(&user_array, size, 10);

回答by Richard Pennington

You're changing the value of user_array locally. The value is lost when the function returns. Pass a pointer to the user_array pointer instead.

您正在本地更改 user_array 的值。当函数返回时,该值丢失。改为传递指向 user_array 指针的指针。