简体   繁体   English

在C中为包含动态结构数组的结构重新分配

[英]Realloc in C for structs containing dynamic array of structs

Issues with reallocing the items list. 重新分配项目列表时出现问题。 I am trying to add items into the testList structs items, but i am getting memory address errors when trying to add or print the values for the individual ListItems. 我试图将项目添加到testList结构项目中,但是尝试添加或打印单个ListItems的值时遇到内存地址错误。 Any help would be greatly appreciated. 任何帮助将不胜感激。

struct ListItems
{
    int id;
    int name;
};

struct testList
{
    struct ListItems** items;
    int count;
    int size;
};

struct Test
{
    struct testList* list;
};

void printArray(struct testList* list)
{
    for (int i = 0; i < list->count; i++)
    {
        printf("id=%i, name= %i \n", list->items[i]->id, list->items[i]->name);
        fflush(stdout);
    }
    printf("printing accomplished \n");
    fflush(stdout);
}

void growArray(struct testList* list, struct ListItems* item)
{
    int size = list->size;
    list->items[list->count++] = item;
    struct ListItems** user_array = list->items;
    //printf("array count %i, array size %i \n", list->count, size);
    if (list->size == list->count)
    {
        struct ListItems* temp = realloc(*user_array, (size * 2) * sizeof (struct ListItems));
        if (temp == NULL)
        {
            printf("it's all falling apart! \n");
        }
        else
        {
            *user_array = temp;
            list->size = size * 2;
        }
    }
}

/*
 *
 */
int main(int argc, char** argv)
{

    struct Test* test = (struct Test*) malloc(sizeof (struct Test));
    test->list = (struct testList*) malloc(sizeof (struct testList));
    test->list->count = 0;
    test->list->size = 1;
    test->list->items = (struct ListItems**) malloc(sizeof (struct ListItems*));

    for (int i = 0; i < 32; i++)
    {
        struct ListItems* item = (struct ListItems*) malloc(sizeof (struct ListItems));
        item->id = i;
        item->name = i;
        growArray(test->list, item);
    }
    printArray(test->list);
    for (int j = 0; j < sizeof (test->list->items); j++)
    {
        free(test->list->items[j]);
    }
    free(test->list->items);
    free(test->list);
    free(test);
}

Your growArray() needs to update list->items . 您的growArray()需要更新list->items In current code, it will point forever to an 1-element sized area only. 在当前代码中,它将永远仅指向1元素大小的区域。

EDIT: 编辑:

your realloc() allocates for sizeof (struct ListItems)) but pointer holds pointers, not elements. 您的realloc()分配了sizeof (struct ListItems))但指针包含指针,而不包含元素。

I would write: 我会写:

void growArray(struct testList* list, struct ListItems* item)
{
        if (list->size <= list->count) {
              size_t new_size = 2 * list->size;
              struct ListItems** temp = realloc(list->items, new_size * sizeof temp[0]);
              assert(temp);

              list->size = new_size;
              list->items = temp;
        }

        list->items[list->count] = item;
        ++list->count;
}

With this, you do not need the initial list->items = malloc(...) in main() but can assign NULL . 这样,您就不需要main()的初始list->items = malloc(...) ,而是可以分配NULL

EDIT: 编辑:

for (int j = 0; j < sizeof (test->list->items); j++)

does not make sense; 没有道理; you probably want to j < test->list->count . 您可能想要j < test->list->count

The problem starts with the declaration of struct testList . 问题始于struct testList的声明。 A pointer to an array of items should have just one * : 指向items数组的指针应该只有一个*

struct testList
{
    struct ListItems* items;   // Changed from pointer-to-pointer
    int count;
    int size;
};

This will force some other changes in the code. 这将迫使代码进行其他一些更改。

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

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