当我在函数中声明一个变量时,我正在使用一些内存。当函数完成它的工作时,内存是否被释放?
最佳答案
所有自动存储变量在超出范围时都将被释放,并且您必须明确说明动态分配的变量:
void foo()
{
int x;
int* y = new int;
//You get a memory leak with each call to foo without the following line
delete y;
} //x is freed here
关于c++ - 函数变量到哪里去了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20999595/