【发布时间】:2016-05-30 06:08:13
【问题描述】:
我被要求在 C++ 中编写两个函数来分配和释放 int 数组。
int* allocate(int size){
return new int[size];
}
void deallocate(int *pt){
delete pt;
pt = NULL;
}
我想出了上面的两个函数。
有谁知道有没有更好的方法在 C++ 中编写函数来分配/解除分配 int 数组?
【问题讨论】:
-
我怀疑有没有更好的方法
-
delete pt;是错误的。必须是delete [] pt; -
在 deallocate 中使用 int&* pt 将指针设置为 NULL。否则外面是看不到的
-
@thorsan 我想你的意思是相反的,检查我的答案。
标签: c++ pointers new-operator delete-operator dynamic-allocation