【问题标题】:Realloc in C for structs containing dynamic array of structsC中的Realloc用于包含动态结构数组的结构
【发布时间】:2013-12-04 14:26:52
【问题描述】:

重新分配项目列表的问题。我正在尝试将项目添加到 testList 结构项目中,但是在尝试添加或打印单个 ListItem 的值时出现内存地址错误。任何帮助将不胜感激。

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);
}

【问题讨论】:

  • 不要强制返回 malloc。你有什么样的错误?
  • test-&gt;list-&gt;items[0] 的区域未被保留。
  • 您可能想尝试某种 malloc 调试工具或软件包。您似乎正在释放尚未分配的内存,并且很难找到此类错误。
  • 出现的主要问题是在 printArray 期间。数组中的某些值不正确。例如:id=0,name=0,则 id=66656,name=6

标签: c dynamic realloc


【解决方案1】:

问题从struct testList 的声明开始。指向items 数组的指针应该只有一个*

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

这将强制对代码进行一些其他更改。

【讨论】:

    【解决方案2】:

    您的growArray() 需要更新list-&gt;items。在当前代码中,它将永远指向一个只有 1 个元素大小的区域。

    编辑:

    您的realloc() 分配给sizeof (struct ListItems)),但指针保存的是指针,而不是元素。

    我会写:

    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;
    }
    

    有了这个,你不需要main()中的初始list-&gt;items = malloc(...),但可以分配NULL

    编辑:

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

    没有意义;你可能想j &lt; test-&gt;list-&gt;count

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-02
      • 2017-06-08
      • 2015-02-13
      • 1970-01-01
      • 2011-01-04
      • 2014-04-02
      • 2021-01-15
      相关资源
      最近更新 更多