正确分配内存,我还需要分配内存吗?
我目前有以下两个结构:
typpedef struct data1 {
int var1;
data2_t *var2;
...
}
并且:
typedef struct data2 {
int var1;
...
} data2_t;
现在,我正在尝试将 int x
与 data2_t **dataArray
中的值进行比较,如果找到该值在数组中,memAddress
设置为指向该值在内存中的位置:
int main(...) {
data2_t **arrayVar, *memAddress;
....
if(inArray(valToCompare, arrayVar, arraySize, &memAddres) {
...
}
....
return 0;
}
int inArray(int x, data2_t **dataArray, int size, data2_t **memAddress) {
for(int i = 0; i < size; i++) {
if(val == dataArray[i]->var1) {
// Variable is in array
*memAddress = dataArray[i];
return 1;
}
}
return 0;
}
首先,memAddress
是否在函数外指向所需的内存空间?
其次,如果我这样做:
int main(...) {
data2_t **arrayVar, *memAddress;
data1_t **dataSet;
....
if(inArray(valToCompare, arrayVar, arraySize, &memAddress) == 1) {
dataSet[index]->var2 = memAddress;
}
....
return 0;
}
将 data1_t struct
中的 var2
设置为指向先前匹配的数据,并且我是否仍然需要为 < code>var2 如果内存已经分配给有问题的内存?
谢谢
I currently have the following two structs:
typpedef struct data1 {
int var1;
data2_t *var2;
...
}
And:
typedef struct data2 {
int var1;
...
} data2_t;
Right now, I'm trying to compare int x
with a value in data2_t **dataArray
, and if the value is found in the array, memAddress
is set to point to where the value is in memory:
int main(...) {
data2_t **arrayVar, *memAddress;
....
if(inArray(valToCompare, arrayVar, arraySize, &memAddres) {
...
}
....
return 0;
}
int inArray(int x, data2_t **dataArray, int size, data2_t **memAddress) {
for(int i = 0; i < size; i++) {
if(val == dataArray[i]->var1) {
// Variable is in array
*memAddress = dataArray[i];
return 1;
}
}
return 0;
}
First of all, is memAddress
pointing to the desired memory space once out of the function?
Secondly, if I was to then do this:
int main(...) {
data2_t **arrayVar, *memAddress;
data1_t **dataSet;
....
if(inArray(valToCompare, arrayVar, arraySize, &memAddress) == 1) {
dataSet[index]->var2 = memAddress;
}
....
return 0;
}
Will var2
in the data1_t struct
be set to point at the previously matched data, and would I still need to allocate memory for var2
if memory has already been allocated to the memory in question?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,在这种情况下,您不需要为字段 var2 分配内存。唯一需要注意的是分配局部变量的地址(超出范围)或使用指向已释放变量的指针。一个好的做法是仅对实际参数使用寻址运算符,就像在代码中模拟按引用调用一样。如果您的程序中有复杂的指针逻辑,那么可能值得使用垃圾收集器库,例如 Boehm GC 。当没有指针指向分配的内存时,垃圾收集器将通过自动释放分配的内存来防止“释放后使用”和内存泄漏。
No, you don't need to allocate memory for the field var2 in this case. The only things to look out for is assigning addresses of local variables (which go out of scope) or using a pointer to a variable which has been freed. A good practice is to only use the address-of operator for actual parameters like you do in your code to simulate call by reference. If you have complicated pointer logic in your program it may be worth using a garbage collector library like Boehm GC. The garbage collector will prevent "use after free" and memory leaks by automatically freeing allocated memory when there are no pointers to it.