函数变量在哪里?

Lie*_*mai 1 c++ memory

当我在函数中声明变量时,我正在使用一些内存.当函数完成它的工作时,内存是否被释放?

Luc*_*ore 6

当超出范围时,将释放所有自动存储变量,并且您必须明确动态分配的变量:

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
Run Code Online (Sandbox Code Playgroud)

  • @Kevin:你不能分配变量.您只能分配对象. (2认同)