c中char数组和字符串的内存位置

Sad*_*lad 2 c c-strings

字符串和字符数组存储在哪里?

int main ()
{
    int a = 0; //This should be stack
    char* p = "hello"; // why this is on the static?
    char k[10] = "hello"; //on the stack?
}
Run Code Online (Sandbox Code Playgroud)

一本教科书上说char指针(Char* a)会存储在静态内存中,从我对“静态内存”的理解来看,只有这2个会存储在静态内存中:

int a=0;// will on the static
int main()
{
    static xxxxx; //will on the static.
}

Run Code Online (Sandbox Code Playgroud)

edd*_*kuo 5

通过6.7.8.2,该字符串"hello"中char *p = "hello"的字符串字面量。
字符串文字通常位于.rodata, 以防止修改。此外,全局变量位于.data节中。

  • 如果在类 Unix 上,@SadSalad 可以使用“objdump -s -j .rodata a.out”(或真正的程序名称代替 a.out)来查看它。 (2认同)