views:

436

answers:

4

Is the pointer returned by the following function valid?

const char * bool2str( bool flg )
{
    return flg ? "Yes" : "No";
}

It works well in Visual C++ and g++. What does C++ standard say about this?

+10  A: 

This code is perfectly valid and conformant. The only "gotcha" would be to ensure that the caller doesn't try to free the string.

JSBangs
"This code is perfectly valid and conformant." Why?
Alexey Malistov
There is no need. The literals are 'const char []' that are never heap allocated. The returned pointer will be valid during the whole program execution.
David Rodríguez - dribeas
There is no "'const char []'"
Alexey Malistov
Trying to release the memory via `free` or `delete` should cause a diagnostic (parameter type mismatch). Hence, you are safe there as well.
dirkgently
"never heap allocated" Why? Any references.
Alexey Malistov
@Alexey Malistov: The effective type of string literals is array of size N const characters.
dirkgently
Alexey Malistov: The standard refers to this as "static storage". See 2.13.4 of draft N-4411. See Annex C, why the type is static.
dirkgently
+6  A: 

This code is valid and standard compliant.

String literals are stored in read-only memory, and the function just gets the address of the chosen string.

C++ standard (2.13.4) says :

An ordinary string literal has type “array of n const char” and static storage duration

They key to understand your problem here, is the static storage duration : string literals are allocated when your program launch, and last for the duration of the program. Your function just gets the address and returns it.

Samuel_xL
**Why? Any referenes?**
Alexey Malistov
Yes. i've edited
Samuel_xL
It is impossible to accept two answers. :(
Alexey Malistov
+13  A: 

On storage duration:

2.13.4 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

read in conjunction with 3.7.1

3.7.1.

All objects which do not have dynamic storage duration, do not have thread storage duration, and are not local have static storage duration. The storage for these objects shall last for the duration of the program (3.6.2, 3.6.3).

On type:

Annex C

Subclause 2.13.4:

Change: String literals made const The type of a string literal is changed from “array of char ” to “array of const char.” The type of a char16_t string literal is changed from “array of some-integer-type ” to “array of const char16_t.” The type of a char32_t string literal is changed from “array of some-integer-type ” to “array of const char32_- t.” The type of a wide string literal is changed from “array of wchar_t ” to “array of const wchar_t.”

Rationale: This avoids calling an inappropriate overloaded function, which might expect to be able to modify its argument.

Effect on original feature: Change to semantics of well-defined feature. Difficulty of converting: Simple syntactic transformation, because string literals can be converted to char*; (4.2). The most common cases are handled by a new but deprecated standard conversion: char* p = "abc"; // valid in C, deprecated in C++ char* q = expr ? "abc" : "de"; // valid in C, invalid in C++

How widely used: Programs that have a legitimate reason to treat string literals as pointers to potentially modifiable memory are probably rare.

Dynamically allocated (the word 'heap' is never used in context of an area of memory AFAIK in the standard) memory requires a function call that can happen as early as main much after the static memory is allocated.

dirkgently
+1 for correct answer, but your quote from Standard is not exact (at least 2.13.4).
Kirill V. Lyadvinsky
A quote about the standard usage of C-Strings may also be usefull. In the C world it is more standard to return a char* pointer that was dynamically allocated and thus is required to be released. If you do not follow this convention then your code will not work well with existing libraries that assume this convention.
Martin York
@Kirill V. Lyadvinsky: This is from the C++0x draft N-4411 (mentioned in a comment by me).
dirkgently
+2  A: 

Technically Yes it is valid.
The strings have static storage durataion.

But that is not the whole story.

These are C-Strings. The convention in C-Libraries and funcctions is to return a dynamically allocated string that should be freed. ie A pointer returned is implicitly passing ownership back to tha caller (As usuall in C there are also exceptions).

If you do not follow these conventions you will confuse a lot of experienced C-Developers that would expect this convention. If you do not follow this standard expectation then it should be well documented in the code.

Also this is C++ (as per your tags). So it is more conventional to return a std::string. The reason for this is that the passing of ownership via pointers is only implied (and this lead to a lot of errors in C code were the above expectation was broken but documented, unfortunately the documentaiton was never read by the user of the code). By using a std::string you are passing an object and their is no longer any question of ownership (the result is passed back as a value and thus yours), but because it is an object there is no questions or issues with resource allocation.

If you are worried about effeciency I think that is a false concern.

If you want this for printing via streams there is already a standard convention to do that:

std::cout << std::boolalpha << false << std::endl;
std::cout << std::boolalpha << true  << std::endl;
Martin York