【发布时间】:2010-06-03 13:41:45
【问题描述】:
好的previous的问题已经回答清楚了,但是我发现了另一个问题。
如果我这样做会怎样:
char *test(int ran){
char *ret = new char[ran];
// process...
return ret;
}
然后运行它:
for(int i = 0; i < 100000000; i++){
string str = test(rand()%10000000+10000000);
// process...
// no need to delete str anymore? string destructor does it for me here?
}
所以把char*转成字符串后,就不用担心删除了?
编辑:作为回答,我必须 delete[] 每个 new[] 调用,但在我的情况下,由于指针丢失,这是不可能的,所以问题是:我如何转换 char正确串起来?
【问题讨论】:
-
回复:编辑...不要丢失指针(根据我下面的回答)或(更好)不要
new指针,只需使用std::string保持细绳。为什么要自己做内存分配?
标签: c++ string char new-operator delete-operator