有内存泄漏吗?
const char * pname = "NAME";
char * temp = new char[strlen(Name) + 64];
sprintf(temp,"%s", pname);
delete [] temp; // is there any memory leaks because now length of temp is 4.
Run Code Online (Sandbox Code Playgroud)
删除(修改)字符串文字(pname)是未定义的行为.此外new/delete,new []/delete[]仅用于配对.
std::string如果可以,建议使用,让你std::string管理内存分配/释放.
std::string pname("NAME");
std::string temp(pname);
std::cout << pname << std::endl;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
102 次 |
| 最近记录: |