C++ string literal data type storage

前端 未结 8 1006
一生所求
一生所求 2020-11-30 10:03
void f()
{
    char *c = \"Hello World!\"
}

Where is the string stored? What\'s the property of it? I just know it is a constant, what else? Can I

相关标签:
8条回答
  • 2020-11-30 10:41

    The string is stored in the data area of the program. This is completely compiler, executable format, and platform dependent. For example, an ELF binary places this in a different location than a Windows executable, and if you were compiling for an embedded platform this data might be stored in ROM instead of RAM.

    Here's an illustration of the layout of the ELF format:

    ELF Layout

    Your string data would most likely be found in the .data or .text sections, depending on compiler.

    You can certainly return it from inside the function body. Just check with your implementation to verify that it is random access, as many implementations won't let you overwrite it.

    0 讨论(0)
  • 2020-11-30 10:42

    §2.14.15 String Literals, Section 7

    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.

    0 讨论(0)
提交回复
热议问题