Scope of (string) literals

前端 未结 7 1341
半阙折子戏
半阙折子戏 2020-11-27 18:13

I always try to avoid to return string literals, because I fear they aren\'t defined outside of the function. But I\'m not sure if this is the case. Let\'s take, for example

7条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 19:04

    This is valid in C (or C++), as others have explained.

    The one thing I can think to watch out for is that if you're using dlls, then the pointer will not remain valid if the dll containing this code is unloaded.

    The C (or C++) standard doesn't understand or take account of loading and unloading code at runtime, so anything which does that will face implementation-defined consequences: in this case the consequence is that the string literal, which is supposed to have static storage duration, appears from the POV of the calling code not to persist for the full duration of the program.

提交回复
热议问题