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

后端 未结 4 990
梦毁少年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:38

    Constant strings are usually placed with program code, which is neither heap nor stack (this is an implementation detail). Only one copy will exist, each time the function returns it will return the same pointer value (this is guaranteed by the standard). Since the string is in program memory, it is possible that it will never be loaded into memory, and if you run two copies of the program then they will share the same copy in RAM (this only works for read-only strings, which includes string constants in C).

提交回复
热议问题