LZF

Does following code return a safe character string

char * GetStr (int n)

{

char *p = NULL;

if ( n >0 )

p = "Something";

return p;

}



Re: Visual C++ General Character pointer

Carl Daniel

"safe" for doing what

The returned pointer points to a character literal, which is likely to be placed in read-only memory by the compiler. The returned pointer will be valid forever - the character literal outlives the function scope in which it appears.

So, as long as all the caller of this function does with the pointer is to READ the string it points to, then yes, it's safe.






Re: Visual C++ General Character pointer

Simple Samples

I think

Code Snippet

char * GetStr (int n)

should be

Code Snippet

const char * GetStr (int n)