#include <iostream>
int main() {
const char* s = nullptr;
{
s = "inner";
}
std::cout << s << std::endl;
return 0;
}
“内”存放在内存的什么地方?
Valgrind 没有提示。是否存储在数据段中?
谢谢。
最佳答案
根据C++标准
8 Ordinary string literals and UTF-8 string literals are also referred to as narrow string literals. 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 (3.7).
所以尽管你的程序中的字符串字面值被用在 main 的内部 block 中
{
s = "inner";
}
它一直存在,直到程序执行完毕,进程的所有内存都将被释放。
通常字符串字面量放在只读数据段中。
关于c++ - 堆栈上的 Const C 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29627441/