This is in reference to the discussion in this topic here
How to have a char pointer as an out parameter for C++ function
In the code below, where do I free the memory of pTemp? Is it not required?
Would things would have changed in anyway if instead of array of chars there was array of integers?
void SetName( char **pszStr )
{
char* pTemp = new char[10];
strcpy(pTemp,"Mark");
*pszStr = pTemp;
}
int main(int argc, char* argv[])
{
char* pszName = NULL;
SetName( &pszName );
cout<<"Name - "<< pszName << endl;
delete [] pszName;
cin.get();
return 0;
}
pTemp, the variable itself, is deallocated once the function exits since it was allocated on the stack in the first place. The array whose pointer was put intopTemppersists outside the function via the argument passed to it,pszName. When the value in this variable is deleted, the heap memory used by the array will be freed.