Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: do all string literals have static storage duration? [duplicate]

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?
like image 281
blue_note Avatar asked Jun 19 '19 12:06

blue_note


People also ask

Are string literals static in C?

String literals have static storage duration, and thus exist in memory for the life of the program.

How are string literals stored in memory?

Strings are stored on the heap area in a separate memory location known as String Constant pool. String constant pool: It is a separate block of memory where all the String variables are held. String str1 = "Hello"; directly, then JVM creates a String object with the given value in a String constant pool.

Are string literals stored in stack or heap C?

String literals are not stored in the heap or the stack, they are stored directly in your program's binary.

Are string literals constant?

String constants, also known as string literals, are a special type of constants which store fixed sequences of characters. A string literal is a sequence of any number of characters surrounded by double quotes: "This is a string."


1 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.

like image 96
Vlad from Moscow Avatar answered Sep 26 '22 12:09

Vlad from Moscow