Do pointers to string literals remain valid after

2019-01-11 23:14发布

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?

4条回答
等我变得足够好
2楼-- · 2019-01-11 23:43

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;
查看更多
老娘就宠你
3楼-- · 2019-01-11 23:47

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

查看更多
放荡不羁爱自由
4楼-- · 2019-01-11 23:52

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.

查看更多
Emotional °昔
5楼-- · 2019-01-11 23:52

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.

查看更多
登录 后发表回答