<分区>
如果有这样一个函数:
int func1() {
int status = func2();
// Do something with status.
return status;
}
在这个函数的过程中,一个类型为 int 的变量 status 被分配内存,在这个函数的过程中,变量被使用,然后从 func1 返回。
我的问题是,什么时候为这个原语收集内存垃圾,还是永远保留在内存中?
<分区>
如果有这样一个函数:
int func1() {
int status = func2();
// Do something with status.
return status;
}
在这个函数的过程中,一个类型为 int 的变量 status 被分配内存,在这个函数的过程中,变量被使用,然后从 func1 返回。
我的问题是,什么时候为这个原语收集内存垃圾,还是永远保留在内存中?
最佳答案
在您的示例中,status 变量分配在堆栈中,将在 func1 返回后立即释放。
关于c++ - 原始变量是否在 C++ 中被垃圾收集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25280070/