当我创建一个返回 char* 或 const char* 的函数时,是否假定调用函数在完成返回值后必须解除分配?否则,它将如何被释放?我正在寻找一些其他代码,它调用一个返回 char* 的函数,并且调用函数中没有删除语句。例子:
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,我不确定如何写这个。我假设这必须删除?有没有更好的写法?