字符串和char数组存储在哪里?
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?
}
一本教科书说char指针(Char * a)将存储在静态存储器上,据我对“静态存储器”的理解,只有这2个将存储在静态存储器中:
int a=0;// will on the static
int main()
{
static xxxxx; //will on the static.
}
根据6.7.8.2,"hello"
中的字符串char *p = "hello"
是字符串文字。字符串文字通常位于.rodata
中,以防止修改。此外,全局变量位于.data
部分。
a
将在堆栈中。 p
本身将位于堆栈中。但是,它指向的数据将位于内存的只读部分(而不是堆栈)中。