Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is returning a reference to a string literal a reference to a temporary?

A regular string string literal has the following definition:

Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. A narrow string literal has type “array of n const char”, where n is the size of the string as defined below, and has static storage duration (3.7).

I'm assuming because it has static storage duration and that they're typically placed in ROM, it really isn't a big deal if there's a dangling reference to it. The following code emits a warning

const char* const & foo()
{
    return "Hello";
}
// warning: returning reference to temporary [-Wreturn-local-addr]

But this is fine, even without the static keyword

const char* const & foo()
{
    const char* const & s = "Hello";
    return s;
}

So what is the difference between the two?

like image 863
user4716387 Avatar asked Mar 26 '15 12:03

user4716387


People also ask

Can you return a string literal?

Strings in C are arrays of char elements, so we can't really return a string - we must return a pointer to the first element of the string. All forms are perfectly valid. Note the use of const , because from the function I'm returning a string literal, a string defined in double quotes, which is a constant.

Why is it called a string literal?

A "string literal" is a sequence of characters from the source character set enclosed in double quotation marks (" "). String literals are used to represent a sequence of characters which, taken together, form a null-terminated string. You must always prefix wide-string literals with the letter L.

Why can't you make a reference to a literal?

A literal just exists. This view makes that there is no address for a pointer to refer to when it would point to a literal and for that reason, pointers to literals are forbidden.

Can you return a string literal in C?

It's perfectly valid to return a pointer to a string literal from a function, as a string literal exists throughout the entire execution of the program, just as a static or a global variable would.


3 Answers

The quote you've posted says that

A narrow string literal has type “array of n const char”

That is, the type of "Hello" is const char[6].

Your code is returning a reference to a const char *. This means the array-to-pointer conversion must be applied to the string literal, resulting in a prvalue (= temporary) of type const char *. You then bind this to a reference and return that reference. The reference becomes dangling as soon as the function's scope ends and the temporary pointer is destroyed.

like image 163
Angew is no longer proud of SO Avatar answered Oct 15 '22 08:10

Angew is no longer proud of SO


There is no difference. In both cases, you return a reference to a pointer that no longer exists.

That the pointee (data) still exists forever is irrelevant.

like image 33
Lightness Races in Orbit Avatar answered Oct 15 '22 09:10

Lightness Races in Orbit


const char* const & s = "Hello";

Here the variable is created on the stack... and that variable (which happens to be a pointer) points to a memory location where the string-literal is stored. You're not returning the string-literal itself; you're rather returning the reference to the variable will be destroyed soon as a result of stack-unwinding. Hence the returning the reference to such a variable is dangerous, as it is a temporary object.

like image 41
Nawaz Avatar answered Oct 15 '22 10:10

Nawaz