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?
str
points to the actual string literal (which has static duration), why do I get an error?str
, where does the original string literal go? does it remain in memory with no reference to it?String literals have static storage duration, and thus exist in memory for the life of the program.
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.
String literals are not stored in the heap or the stack, they are stored directly in your program's binary.
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."
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With