Lem*_*ons 1 c c++ malloc free garbage-collection
在C和C++中释放内存时,我只需要内存地址还是需要任何特定的变量?
所以如果我要做的事情如下:
int* test()
{
int* x = new int(5);
return x;
}
int main(int argc, char** argv)
{
int* y = test();
delete y;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会导致内存泄漏吗?谢谢!
不,那里不会有任何泄漏,但话说再也没有泄漏
int test()
{
return 5;
}
int main(int argc, char** argv)
{
int y = test();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果可以,请避免动态分配.