【问题标题】:How to allocate struct pointer inside a struct dynamically?如何动态分配结构内的结构指针?
【发布时间】:2021-08-09 03:44:35
【问题描述】:

我目前有一个结构 Struct1,它有一个指向 Struct2 的指针,该指针根据代码中的某些条件被多次分配。我试图以test->Struct2Pair[i] = malloc(sizeof(struct Struct2));这种方式分配它,但它似乎失败了。知道我做错了什么吗?

这是我正在尝试做的简化版本。

struct Struct2 {
    int x;
    int y;
};

struct Struct1 {
    struct Struct2 *Struct2Pair;
    int val;
};

int main()
{

    struct Struct1 *test = malloc(sizeof(struct Struct1));
    for ( int i = 0; i < 5; i++ )
    {
        test->Struct2Pair[i] = malloc(sizeof(struct Struct2));
    }
    return 0;
}

谢谢!

【问题讨论】:

  • Struct2Pair 未初始化——你不能像一个有效的数组一样开始给它赋值。至少,您需要test-&gt;Struct2Pair = malloc(5 * sizeof(struct Struct2));,它为struct Struct2 类型的5 个元素分配内存。但是,如果您只想对数组进行硬编码,为什么不首先将其定义为 struct Struct2 Struct2Pair[5];
  • @paddy 就像我说的那样,5 只是一个例子。该数字根据一些输入和一些计算是可变的。我只想知道每次满足某些条件时如何 malloc test->Struct2Pair 。可以是 1... 可以是 100。我不确定该怎么做。
  • @JaneDoe:当您执行test-&gt;Struct2Pair[i] 时,您正在尝试获取Struct2Pair 的访问内存位置,这并没有指向任何有意义的东西。
  • 如果您需要知道数组包含多少项,那么您需要将该值存储在某处(可能是Struct1 中的一个额外变量)。否则,您无法知道数组是否足够大(如果您打算稍后使用 realloc 或其他方式调整它的大小)。我坚持关于如何使用malloc 分配数组的评论。你这样做是错的。这里唯一的修改是使用可变大小而不是硬编码5,并将值存储在某处。
  • 它是运行时的,在分配 Struct2Pair 时是未知的。如果满足某些条件,它会不断地被一一分配。在这种情况下,您的解决方案是否仍然有效?

标签: c struct


【解决方案1】:

为了使Struct2Pair指向Struct2类型的连续5个内存对象,你必须分配一个5*sizeof(Struct2)。因此在代码的后半部分,您可以将其作为大小为 5 的数组进行访问。

你必须做null检查malloc的返回值。

    struct Struct1 *test = malloc(sizeof(struct Struct1));

        //   |-----change is here and here--------------|
        //   v                                          v
        test->Struct2Pair = malloc(sizeof(struct Struct2) * 5);
    
    //access can be as follows for all the index from 0  to 4
    test->Struct2Pair[2].x = 10;
    test->Struct2Pair[2].y = 20;

要获得更好的可视化效果,请参阅 thisthis

【讨论】:

    【解决方案2】:

    您遇到的问题位于for 循环内以及test-&gt;Struct2Pair 数组中可用Struct2s 的数量。

    在将Struct2s 放入之前,您没有为test-&gt;Struct2Pair 分配任何空间

    test->Struct2Pair[i] = malloc(sizeof(struct Struct2));
    

    然后循环遍历更多索引,然后可用于test-&gt;Struct2Pair 数组

    for ( int i = 0; i < 5; i++ )
    

    您可以考虑添加另一个变量来存储test-&gt;Struct2Pair 数组的大小:

    int size = 5;
    struct Struct1 *test = malloc(sizeof(struct Struct1));
    test->Struct2Pair = malloc(size * sizeof(struct Struct2));
    for ( int i = 0; i < size; i++ )
    {
        test->Struct2Pair[i].x = 0;
        test->Struct2Pair[i].y = 0;
    }
    

    参考mallochttps://en.cppreference.com/w/c/memory/malloc

    【讨论】:

    • 这是不正确的。 Struct2Pair 未初始化。访问它将是 UB。
    • 回答、获取和适应。我盯着这个问题,然后调整了答案。只需打开一个shell并测试调整后的代码即可编译并运行。
    • 没问题@JaneDoe。我注意到在没有任何参考的情况下提到了 realloc。如果您确实选择了这条路线,这里是它的使用的一般示例:en.cppreference.com/w/c/memory/realloc
    • 这里是动态内存管理的另一个参考:eskimo.com/~scs/cclass/notes/sx11.html
    • 我确实最终采用了 realloc 方式。先test-&gt;Struct2Pair = calloc(0, sizeof(struct Struct2)); 然后test-&gt;Struct2Pair = realloc(test-&gt;Struct2Pair, (i+1) * sizeof(struct Struct2)); 这似乎解决了我的问题!
    猜你喜欢
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 2015-07-04
    • 1970-01-01
    • 1970-01-01
    • 2019-10-20
    相关资源
    最近更新 更多