cjm*_*ria 0 c++ valgrind c++11
我有一段非常简单的代码:
#include <iostream>
int main()
{
int *val = new int;
*val = 12;
std::cout << *val << std::endl;
delete &val;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我在这上面运行Valgrind时,我收到以下错误:
SUMMARY: 3 errors from 3 contexts (suppressed: 8 from 8)
1 errors in context 1 of 3:
Invalid free() / delete / delete[] / realloc()
at 0x1000ABB6D: free (vg_replace_malloc.c:533)
by 0x100000D1E: main (pointers.cpp:8)
Address 0x1048a09f0 is on thread 1's stack
in frame #1, created by main (pointers.cpp:4)
Run Code Online (Sandbox Code Playgroud)
我如何删除有val什么问题?
invalid free()如果您尝试释放无效内存,则会收到错误消息.
delete &val;
Run Code Online (Sandbox Code Playgroud)
在这里你试图删除的地址val,而不是内存val点,这是错误的.请尝试以下方式.
delete val;
Run Code Online (Sandbox Code Playgroud)