34

I know this is wrong:

char* getSomething() {
    char szLocal[5];
    /* Put something in the char array somehow */
    return szLocal;
}

...because szLocal can be destroyed sometime after the function returns.

But is this ok?

char* getSomethingElse() {
    return "something else";
}
2
  • 3
    See C String literals: Where do they go?. Commented Jan 29, 2011 at 11:15
  • 8
    In the first example, szLocal is destroyed not "sometime after", but exactly at the very moment the function returns, so the returned pointer is already invalid. True, the data it points at will remain the same for "some time", but the function's stack frame is freed already, and the memory becomes available for use by anyone who needs to create some local variables or call another function. Commented Jan 29, 2011 at 11:21

3 Answers 3

29

That is actually OK. The string literal is usually allocated in an immutable memory area that remains available for as long as your program is running.

See also the answers to when does c/c++ allocate string literals.

Sign up to request clarification or add additional context in comments.

3 Comments

I agree with romkyns. However, we don't know what system you're working on? There may be some out there that don't work like that. For your own peace of mind, you could try purposefully stamping over the memory where we are suggesting the string is NOT living. i.e. the stack. Get stuff on the stack by either declaring and assiging some more local variables, or call some other functions with a few parameters. Ideally, call another function with a few parameters that itself declares and assigns some local variables.
OMG this place is awesome. 10 useful replies in 10 minutes. Thanks all. (I'm on x64 windows using vs2010 btw.)
String literals are guaranteed to live for the span of the application. It might not be in read-only/immutable memory, but they're guaranteed to exist for the lifetime of the application, so returning a pointer to one is fine.
8

It's ok in terms of allocation: the string literal is implicitly static. It's not ok to return a non-const pointer to a literal.

If you want to return a modifiable (non-const) string, declare it a static char[]. Or better, return a copy:

return strdup("something else");

Don't forget to free afterwards. strdup is non-ISO but available almost everywhere (except MSVC, I believe).

5 Comments

strdup not available on msvc ? are you sure ?
@Arabcoder: no, I'm not sure, and please correct me if I'm wrong. I don't do MSVC.
Good point about const. Wouldn't a (good) compiler catch this though - an attempt to cast const char to non-const char?
@romkyns: GCC 4.5 doesn't even complain with -Wall: as Jens Gustedt points out below nominolo's answer, a literal isn't const. (G++ complains, but still compiles the module, and then we're out of the C world.)
I see. I suppose if I were to be pedantic then returning a literal as non-const is not an outright error then. Still, very C-like to let you shoot yourself in the foot by making literals non-const.
5

The type of a string literal is static char[], but immutable. A string literal represents a pointer to statically allocated memory. Therefore:

  1. It is perfectly fine to return a such a pointer.

  2. Your function's return type should be compatible with const char*, i.e., return type char * may give you trouble later on.

  3. If your function may return both a literal or malloced string you have to be very careful about memory management. freeing a string literal probably will segmentation fault.

3 Comments

Actually, the type is static const char[], i.e. closer to const char *const. You can't increment or decrement a literal.
@larsman: No, unfortunately for historical reasons its type is not const qualified. But you don't have the right to change it :(( So it is certainly a good idea to declare the return value const but the language doesn't enforce this.
@larsman, @Jens: Thanks, I stand corrected. I should have checked before submitting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.