Scope of (string) literals

前端 未结 7 1337
半阙折子戏
半阙折子戏 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 18:48

    It's really important to make note of the undefined results that Brian mentioned. Since you have declared the function as returning a const char * type, you should be okay, but on many platforms string literals are placed into a read-only segment in the executable (usually the text segment) and modifying them will cause an access violation on most platforms.

提交回复
热议问题