Heap or Stack? When a constant string is referred in function call in C++

后端 未结 4 1008
梦毁少年i
梦毁少年i 2020-12-19 09:03

Consider the function:

char *func()
{
    return \"Some thing\";
}

Is the constant string (char array) \"So

4条回答
  •  忘掉有多难
    2020-12-19 09:25

    String literal "Some thing" is of type const char*. So, they are neither on heap nor on stack but on a read only location which is a implementation detail.

    From Wikipedia

    Data

    The data area contains global and static variables used by the program that are initialized. This segment can be further classified into initialized read-only area and initialized read-write area. For instance the string defined by char s[] = "hello world" in C and a C statement like int debug=1 outside the "main" would be stored in initialized read-write area. And a C statement like const char* string = "hello world" makes the string literal "hello world" to be stored in initialized read-only area and the character pointer variable string in initialized read-write area. Ex: static int i = 10 will be stored in data segment and global int i = 10 will be stored in data segment

提交回复
热议问题