New*_*bie 3 c++ new-operator delete-operator
这对我来说有点不清楚......所以,如果我有一个功能:
char *test(int ran){
char *ret = new char[ran];
// process...
return ret;
}
Run Code Online (Sandbox Code Playgroud)
然后多次调用它:
for(int i = 0; i < 100000000; i++){
char *str = test(rand()%10000000+10000000);
// process...
// delete[] str; // do i have to delete it here?
}
Run Code Online (Sandbox Code Playgroud)
所以问题是,我必须delete[]为每次new[]通话使用吗?
是的,否则你会有内存泄漏.
但是,分配在一个函数中并在另一个函数中释放并不是最好的主意.为什么在未分配的循环和传递指针test-这保持new和delete代码在一起.
| 归档时间: |
|
| 查看次数: |
2983 次 |
| 最近记录: |