Consider the function:
char *func()
{
return "Some thing";
}
Is the constant string
(char
array) "Some thing"
stored in the stack as local to the function call or as global in the heap?
I'm guessing it's in the heap.
If the function is called multiple times, how many copies of "Some thing"
are in the memory? (And is it the heap or stack?)
Neither, its in the static section of the program. Similar to having the string as a global variable. There is only ever one copy of the string within the translation unit.
Constant strings are usually placed with program code, which is neither heap nor stack (this is an implementation detail). Only one copy will exist, each time the function returns it will return the same pointer value (this is guaranteed by the standard). Since the string is in program memory, it is possible that it will never be loaded into memory, and if you run two copies of the program then they will share the same copy in RAM (this only works for read-only strings, which includes string constants in C).
Neither on the heap, nor on stack, it is part of the so-called init section in the executable image (COFF). This is loaded into memory and contains stuff like strings.
String literal "Some thing" is of type
const char*
. So, they are neither on heap nor on stack but on a read only location which is a implementation detail.From Wikipedia