i have a huge problem allocating memory in C
在C中分配内存有一个很大的问题
i have this struct
我有这个结构体
typedef struct{
int x;
int y;
}T;
i want to create a function that dynamically adds a structs to a pointer. something like:
我想创建一个函数来动态地向指针添加结构。喜欢的东西:
int main()
{
T* t;
f(&t);
free(t);
}
up to this point i think everything is ok, now the function is where i get lost
到目前为止,我认为一切都没问题,现在的功能就是我迷失的地方。
void f(T** t)
{
T t1;
T t2;
T t3;
//first i malloc
*t=malloc(sizeof(T)*T_MAX_SIZE);//i want another function to make the array bigger, but this is not as important as the problem
t1.x=11;
t1.y=12;
t2.x=21;
t2.y=22;
t3.x=31;
t3.y=32;
//now i want to copy the values from t1,t2,t3 to t[0],t[1],t[2]
memcpy(&(*t[0]),&t1,sizeof(T));
memcpy(&(*t[1]),&t2,sizeof(T));
memcpy(&(*t[2]),&t3,sizeof(T));
}
i do not know the correct way of copying these structs.
我不知道复制这些结构体的正确方法。
the point of doing this is to use t out of the function (in the main)
这样做的目的是在函数中使用t(主)
many thanks :D
非常感谢:D
2 个解决方案
#1
2
Your memcpy
calls are incorrect.
你的memcpy调用不正确。
In the expression &(*t[0])
, the array index has top precedence, followed by the pointer indirection. So with explicit parenthesis it looks like &(*(t[0]))
.
在表达式&(*t[0])中,数组索引优先级最高,然后是指针间接。因此有了显式括号,它看起来像&(*[0])。
So it first tries to array subscript t
, which is the address of t
in main. In the case of t[0]
it still works, but t[1]
references something past that variable, invoking undefined behavior. You want the array index of what t
points to, which is (*t)[i]
.
它首先尝试数组下标t,也就是主地址t。对于t[0],它仍然可以工作,但是t[1]引用那个变量之后的东西,调用未定义的行为。你想要的是t指向的数组下标,也就是(*t)[i]。
So the memcpy calls should be:
所以memcpy调用应该是:
memcpy(&((*t)[0]),&t1,sizeof(T));
memcpy(&((*t)[1]),&t2,sizeof(T));
memcpy(&((*t)[2]),&t3,sizeof(T));
#2
2
You don't need any copy functions to assign one structure to another - you simply equate them. So if you have
您不需要任何复制函数来将一个结构分配给另一个结构——您只需将它们等同起来。所以如果你有
T var1 = {1, 2};
T var2 = var1;
the whole of var1
is copied to var2
. Amending your (simplified) program:
整个var1被复制到var2。修改你的(简化)计划:
#include <stdio.h>
#include <stdlib.h>
#define T_MAX_SIZE 10
typedef struct{
int x;
int y;
}T;
void f(T** t)
{
T t1;
*t=malloc(sizeof(T)*T_MAX_SIZE);
t1.x=11;
t1.y=12;
(*t)[0] = t1;
}
int main(void) {
T* t;
f(&t);
printf ("Result %d %d\n", t[0].x, t[0].y);
free(t);
return 0;
}
Program output:
项目输出:
Result 11 12
#1
2
Your memcpy
calls are incorrect.
你的memcpy调用不正确。
In the expression &(*t[0])
, the array index has top precedence, followed by the pointer indirection. So with explicit parenthesis it looks like &(*(t[0]))
.
在表达式&(*t[0])中,数组索引优先级最高,然后是指针间接。因此有了显式括号,它看起来像&(*[0])。
So it first tries to array subscript t
, which is the address of t
in main. In the case of t[0]
it still works, but t[1]
references something past that variable, invoking undefined behavior. You want the array index of what t
points to, which is (*t)[i]
.
它首先尝试数组下标t,也就是主地址t。对于t[0],它仍然可以工作,但是t[1]引用那个变量之后的东西,调用未定义的行为。你想要的是t指向的数组下标,也就是(*t)[i]。
So the memcpy calls should be:
所以memcpy调用应该是:
memcpy(&((*t)[0]),&t1,sizeof(T));
memcpy(&((*t)[1]),&t2,sizeof(T));
memcpy(&((*t)[2]),&t3,sizeof(T));
#2
2
You don't need any copy functions to assign one structure to another - you simply equate them. So if you have
您不需要任何复制函数来将一个结构分配给另一个结构——您只需将它们等同起来。所以如果你有
T var1 = {1, 2};
T var2 = var1;
the whole of var1
is copied to var2
. Amending your (simplified) program:
整个var1被复制到var2。修改你的(简化)计划:
#include <stdio.h>
#include <stdlib.h>
#define T_MAX_SIZE 10
typedef struct{
int x;
int y;
}T;
void f(T** t)
{
T t1;
*t=malloc(sizeof(T)*T_MAX_SIZE);
t1.x=11;
t1.y=12;
(*t)[0] = t1;
}
int main(void) {
T* t;
f(&t);
printf ("Result %d %d\n", t[0].x, t[0].y);
free(t);
return 0;
}
Program output:
项目输出:
Result 11 12