C: do all string literals have static storage duration?

Viewed 847

I've been reading in various sources that string literals remain in memory for the whole lifetime of the program. In that case, what is the difference between those two functions

char *f1() { return "hello"; }
char *f2() {
   char str[] = "hello";
   return str;
}

While f1 compiles fine, f2 complains that I'm returning stack allocated data. What happens here?

  • if the str points to the actual string literal (which has static duration), why do I get an error?
  • if the string literal is copied to the local variable str, where does the original string literal go? does it remain in memory with no reference to it?
4 Answers

This

char str[] = "hello";

is a declaration of a local array that is initialized by the string literal "hello".

In fact it is the same as if you declared the array the following way

char str[] = { 'h', 'e', 'l', 'l', 'o', '\0' };

That is the own area of memory (with the automatic storage duration) of the array is initialized by a string literal.

After exiting the function the array will not be alive.

That is the function

char *f2() {
   char str[] = "hello";
   return str;
}

tries to return a pointer to the first element of the local character array str that has the automatic storage duration.

As for this function definition

char *f1() { return "hello"; }

then the function returns a pointer to the first character of the string literal "hello" that indeed has the static storage duration.

You may imagine the first function definition the following way

char literal[] = "hello";
char *f1() { return literal; }

Now compare where the arrays are defined in the first function definition and in the second function definition.

In the first function definition the array literal is defined globally while in the second function definition the array str is defined locally.

if the str points to the actual string literal (which has static duration), why do I get an error?

str is not a pointer. It is a named extent of memory that was initialized by a string literal. That is the array has the type char[6].

In the return statement

return str;

the array is implicitly converted to pointer to its first element of the type char *.

Functions in C and C++ may not return arrays. In C++ functions may return references to arrays.

I've been reading in various sources that string literals remain in memory for the whole lifetime of the program.

Yes.

In that case, what is the difference between those two functions

char *f1() { return "hello"; }
char *f2() {
   char str[] = "hello";
   return str;
}

f1 returns a pointer to the first element of the array represented by a string literal, which has static storage duration. f2 returns a pointer to the first element of the automatic array str. str has a string literal for an initializer, but it is a separate object.

While f1 compiles fine, f2 complains that I'm returning stack allocated data. What happens here?

  • if the str points to the actual string literal (which has static duration), why do I get an error?

It does not. In fact, it itself does not point to anything. It is an array, not a pointer.

  • if the string literal is copied to the local variable str, where does the original string literal go? does it remain in memory with no reference to it?

C does not specify, but in practice, yes, some representation of the string literal must be stored somewhere in the program, perhaps in the function implementation, because it needs to be used to initialize str anew each time f2 is called.

The string that you will see on your stack is not a direct result of the presence of a string literal. The string is stored, in case of ELF, in a separate region of the executable binary called "string table section", along with other string literals that the linker meets during the linking process. Whenever the stack context of the code that actually caused a string to be included is instantiated, the contents of the string in string table section are actually copied to the stack.

A brief reading that you might be interested in: http://refspecs.linuxbase.org/elf/gabi4+/ch4.strtab.html

char str[] = "hello"; is a special syntax which copies the string literal, and your function returns a pointer to this local variable, which is destroyed once the function returns.
char *f1() { return "hello"; } is correct but returning const char* would probably be better.

Related