如何在C/C++中释放数组
发布时间:2020-12-16 09:59:49 所属栏目:百科 来源:网络整理
导读:int main() { // Will this code cause memory leak? // Do I need to call the free operator? // Do I need to call delete? int arr[3] = {2,2,3}; return 0;} 此代码是否会造成内存泄漏? arr在哪里居住?在堆栈或RAM中? 解决方法 在这个计划中 int mai
int main() { // Will this code cause memory leak? // Do I need to call the free operator? // Do I need to call delete? int arr[3] = {2,2,3}; return 0; } >此代码是否会造成内存泄漏? 解决方法
在这个计划中
int main() { // Will this code cause memory leak? // Do I need to call the free operator? // Do I need to call delete? int arr[3] = {2,3}; return 0; } array arr是函数main的局部变量,具有自动存储持续时间.它将在函数完成其工作后被销毁. 函数本身在调用时分配了数组,它将被销毁,退出函数. 没有内存泄漏. 您不应该既不调用C函数也不调用操作符删除[]. 如果程序看起来如下 int main() { int *arr = new int[3] {2,3}; //... delete [] arr; return 0; } 然后你应该编写operator delete [],因为它在函数中显示. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |