Say I have a simple function that returns a C string this way:
const char * getString()
{
const char * ptr = "blah blah";
return ptr;
}
and I call getString() from main() this way:
const char * s = getString();
1) According to gdb, the variable ptr is stored on the stack, but the string pointed by ptr is not:
(gdb) p &ptr
$1 = (const char **) 0x7fffffffe688
(gdb) p ptr
$2 = 0x4009fc "blah blah"
Does this mean that "blah blah" is not a local variable inside getString()?
I guess that if it were a local variable, I would not be able to pass it to my main() function...
But if it's not, where is it stored? On the heap? Is that a "kind of" dynamically memory allocation implemented by the OS every time it hits on a string, or what?
2) If I use an array instead of a pointer, this way:
const char *getString2()
{
const char a[] = "blah blah blah";
return a;
}
the compiler warns me that:
warning: address of local variable ‘a’ returned
(and of course the program compiles, but it doesn't work).
Actually, if I ask gdb, I get
(gdb) p &a
$2 = (const char (*)[15]) 0x7fffffffe690
But I thought that const char * ptr and const char a[] were basically the same thing. Looks like they're not.
Am I wrong? What is exactely the difference between the two versions?
Thank you!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…