当我创建一个返回 char* 或 const char* 的函数时,是否假定调用函数在完成返回值时必须释放返回值?否则,它将如何被释放?我正在寻找其他一些调用返回 char* 的函数的代码,并且调用函数中没有 delete 语句。示例:
char* foo();
void bar()
{
char* result = foo();
//I should have "delete result" here right?
}
编辑:
所以在我的应用程序中,这里是 foo:
LPTSTR GetTempPath(LPCTSTR fileName)
{
LPTSTR tempPath = new TCHAR[500];
GetTempPath(500,tempPath);
printf("Temp Path %ls\n",tempPath);
PathAppend(tempPath, fileName);
_tprintf(_T("New temp path: %s\n"), tempPath);
return tempPath;
}
如果没有新的 TCHAR,我不确定如何写这个。 我假设这必须删除?有没有更好的写法?
最佳答案
如果您使用的是 C++ 而不是 C,您应该使用 std::string
来解决这个问题。
关于c++ - 取消分配从函数返回的 char*,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16970258/