字符串和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.
}
答案 0 :(得分:5)
通过6.7.8.2,"hello"
中的字符串char *p = "hello"
是字符串文字。
字符串文字通常位于.rodata
中,以防止修改。此外,全局变量位于.data
部分。
答案 1 :(得分:1)
a
将在堆栈中。 p
本身将位于堆栈中。但是,指向的数据将位于内存的只读部分(而不是堆栈)中。