Returning const char * from a string literal in C ++?

I usually return std::stringfrom a function because returning const char*requires that the calling server issue an output memory buffer, and this buffer does not change.

But returns const char*valid if it is from a string literal?

const char* generate_c_string() {
    return "ABC";
}

Executing in this way (if it really is) will most likely be faster, since I will not need to dynamically allocate memory for the build std::string.

It is probably valid because it is const char* x = "ABC";valid. Is there a link to the C ++ standard confirming its reliability?

+4
source share
1 answer

This is true for string literals ,

, , .

.

+6

Source: https://habr.com/ru/post/1691082/


All Articles