[英]Dynamically create and initialize an array of pointers to struct in C
Here is an example code of what I'm trying to achieve 这是我要实现的示例代码
typedef struct
{
int x_pos, y_pos;
} A;
typedef struct
{
A **arrayAp;
} B;
int main(void) {
B B;
int n = 10;
A *array[n];
B.arrayAp = array;
for (int i = 0; i < 10; i++)
{
B.arrayAp[i] = malloc(sizeof(A));
B.arrayAp[i]->x_pos = i;
B.arrayAp[i]->y_pos = i + 1;
}
printf("%d",B.arrayAp[0]->x_pos);
}
It works as I want it to work. 它可以按我想要的方式工作。 I can access elements of "array" using "arrayAp" pointer.
我可以使用“ arrayAp”指针访问“ array”的元素。 But when I try to move some of this code to function for example:
但是,当我尝试将其中一些代码移入功能时,例如:
A * makeAnArray()
{
int n = 10;
A *array[n];
return &array;
}
and then assign value that it returns to "arrayAp" 然后分配它返回到“ arrayAp”的值
B.arrayAp = makeAnArray();
Now I can't access first element of the array. 现在,我无法访问数组的第一个元素。 Program just crash.
程序崩溃了。
printf("%d",B.arrayAp[0]->x_pos);
But starting from second element everything works the same. 但是从第二个元素开始,一切工作都是相同的。
But the bigger problem I get when I try to move to function this code: 但是,当我尝试移动此代码的功能时,遇到了更大的问题:
void initializeAnArray(B *B)
{
for (int i = 0; i < 10; i++)
{
B->arrayAp[i] = malloc(sizeof(A));
B->arrayAp[i]->x_pos = i;
B->arrayAp[i]->y_pos = i + 1;
}
}
Seems like it has no effect. 似乎没有效果。 When i'm trying to access array member through "arrayAp" pointer i'm getting some "random" values from memory.
当我尝试通过“ arrayAp”指针访问数组成员时,我从内存中获取了一些“随机”值。
typedef struct
{
int x_pos, y_pos;
} A;
typedef struct
{
A **arrayAp;
} B;
A * makeAnArray()
{
int n = 10;
A *array[n];
return &array;
}
void initializeAnArray(B *B)
{
for (int i = 0; i < 10; i++)
{
B->arrayAp[i] = malloc(sizeof(A));
B->arrayAp[i]->x_pos = i;
B->arrayAp[i]->y_pos = i + 1;
}
}
int main(void) {
B B;
B.arrayAp = makeAnArray();
initializeAnArray(&B);
printf("%d",B.arrayAp[1]->x_pos);
}
Sorry for my poor English. 对不起,我的英语不好。
A * makeAnArray()
{
int n = 10;
A *array[n];
return &array;
}
You are returning a local pointer which dies after the function ends and causes UB. 您将返回一个本地指针,该指针将在函数结束后终止并导致UB。
Moreover return type of your function should be A**
and you should allocate memory with *alloc
and return to ensure every instance is different and is alive after the function call. 此外,函数的返回类型应为
A**
并且应使用*alloc
分配内存并返回以确保每个实例都不同并且在函数调用后仍处于活动状态。
Possible fix 可能的修复
A * makeAnArray()
{
int n = 10;
A *array = malloc(n * sizeof(A));
return array;
}
Your function should be: 您的功能应为:
A * makeAnArray()
{
int n = 10;
//A *array[n]; --> This is local to this function so won't work
A *array = malloc(sizeof(A)*n); //--> Dynamic allocation
return array;
}
The part 那个部分
A * makeAnArray()
{
int n = 10;
A *array[n];
return &array;
}
is not going to work. 不上班。 The array
array
might be allocated on the stack, it cannot be accessed via the return value after termination of makeAnArray
; 数组
array
可能在堆栈上分配,在makeAnArray
终止后makeAnArray
返回值访问它; the result is undefined behaviour. 结果是不确定的行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.