使用新的then设置为null是否会导致内存泄漏?香港专业教育学院尝试以下代码,但不知道是否会导致任何泄漏#include <iostream>using namespace std;int main(){ int* x = new int; int y = 1; x = &y; x = nullptr; // has the memory allocated to x gone now? x =&y; // did i recover what was lost? delete x; return 0;}// cout (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 是的,这是一个泄漏。但是,将nullptr分配给x时不会发生泄漏,而是在它之前的行中发生:x = &y;现在,x指向y的地址,并且不存在对使用new int分配的内存的其他引用。没有对该内存的任何引用,就无法取消分配它。 (adsbygoogle = window.adsbygoogle || []).push({});
10-08 09:44
查看更多