以下 C 代码中的内存泄漏
我如何释放从以下代码泄漏的内存。
struct object_one{
int value;
}*object,object_node;
struct node_one {
void **pointers;
}*node, node_node;
node sample(){
object number;
node node123;
node123 = malloc(sizeof(node_node));
number = malloc(sizeof(object_node));
number->valu = malloc(sizeof(int));
number->value = 9;
node123->pointers[0]=number;
free(number);
return node123;
}
我是否正在采取正确的方法来释放 number
引用的内存。一旦我执行了上述操作,我就会收到错误;
Invalid read of size 4
==15957== at 0x403804: main (abc.c:1255)
==15957== Address 0x540cb50 is 0 bytes inside a block of size 4 free'd
请建议我如何防止这种情况下的内存泄漏?提前致谢。
[编辑]
嗨,实际上上面提到的不是我真正的代码。但我已尽力反映我的代码的结构和语义。实际上我正在实现一个数据结构。 number
是一些临时存储,node123
是我真正的数据库。我想在数据库中分配 number
的值并取消引用它。number
类型是数据库的一个组件。
[EDIT2]
对应于行 1255
的代码正在打印 object i'e value
所保存的值。它看起来像:
object tempObject;
tempObject = search_object(root,50);
[1255] printf("Key is %d ------>value is %d\n",50,tempObject->value);
这里是函数 search_object
搜索与键 50
对应的值。该函数返回与该键关联的正确值,但仍然显示此类错误。
How would i free the memory that is leaking fromt he following code.
struct object_one{
int value;
}*object,object_node;
struct node_one {
void **pointers;
}*node, node_node;
node sample(){
object number;
node node123;
node123 = malloc(sizeof(node_node));
number = malloc(sizeof(object_node));
number->valu = malloc(sizeof(int));
number->value = 9;
node123->pointers[0]=number;
free(number);
return node123;
}
Am I doing the correct way to deallocate the memory referenced by number
.Once i do the above one, i get the error;
Invalid read of size 4
==15957== at 0x403804: main (abc.c:1255)
==15957== Address 0x540cb50 is 0 bytes inside a block of size 4 free'd
Please suggest me how can I prevent the memory leak in this condition? Thanks in advance.
[EDIT]
Hi actually the above mentioned is not the real code I have. But I have tried my best to refelect the sturcutre and semantics of my code. Actually I am implementing a data strcture. number
is some temproary storage and node123
is my real data-base. I would like to assign the value of number
in the database and dereference it.number
type is a component of database.
[EDIT2]
The code corresponding to line 1255
is printing for the value that is holded by the object i'e value
.It looks like:
object tempObject;
tempObject = search_object(root,50);
[1255] printf("Key is %d ------>value is %d\n",50,tempObject->value);
Here the functionsearch_object
searches the value corresponding to the key 50
. The function is returning the proper value associated with the key and still it is showing such error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还需要
free(node123)
。但这不是错误消息告诉您的内容。
您还取消引用未指向有效内存的指针(在
node123->pointers[0] = number 行上) )
。您尚未为指针分配任何内存来指向,因此像这样取消引用将写入内存中的随机区域。You also need to
free(node123)
.But this is not what the error message is telling you.
You are also dereferencing a pointer that doesn't point at valid memory (on the line
node123->pointers[0] = number)
. You have not allocated any memory forpointers
to point at, so dereferencing it like this will write to a random area in memory.看来您忘记释放(node123)了。
对于每个 malloc() 调用,您都必须有一个 free() 调用。
您显示的 valgrind 错误不是内存泄漏,而是已释放的内存区域中的无效读取。
它发生在 abc.c 的第 1255 行。
You forgot to free(node123), it seems.
For each malloc() call you must have a free() call.
The valgrind error you show is not a memory leak, it's an invalid read in a memory region that has been free'd.
It happens in abc.c at line 1255.
你忘记了一个 free(node123);
还有这一行:node123->pointers[0]=number;是个麻烦。
您没有初始化node123内的指针。
You forgot a free(node123);
And also this line: node123->pointers[0]=number; is a trouble.
You didn't initialized pointers inside node123.