Back to me...

Consider this situation:

Code:
// Assuming the function 
// char *getString ();
// is defined somewhere else

char *string = getString();
printf ("string == %s\n", string);
delete [] string;
This will work assuming the [tt]getString[/tt] function goes something like this:
Code:
char* getString(){
   char *string = new char[128];
   sprintf (string, "hello, %d", 234);
   return string;
}
But, if the [tt]getString[/tt] function goes something like this next example, the [tt]delete[] string;[/tt] part will segfault the program

Code:
char *getString (){
   switch (something){
      case 1: return "hello";
      case 2: return "hi";
      case 3: return "ok";
   }
}
This will cause the segfault if the user tries to delete the string.

There are a few ways I can see to get around this. The most obvious is to go and look at the function and determine what needs to be done with the return value (or look in the function's documentation to see if the author tells you).

Or you can re-write the second getString function to be similar to the first (that is, using a [tt]new char [128][/tt] and filling it with sprintf. But this would be less efficient and much more messy if you just want to return constant strings (for example, say you have an error code to string conversion function).

I'm sure there are more options that I'm missing. What are they? There has to be a better/faster/cleaner method than what I'm using. Perhaps change the getString function to constant:
Code:
const char *getString();
What do you think? Which method would be the best?