const char* test(bool i)
const char t = "aa\n";
const char* p = "bbb\n";
int main(array<System::String ^> ^args)
That returns something of sort:
It is clear that <strong>test(false)</strong> returns a pointer to a local variable. The question is that p is also local variable. Why the memory for "bbb\n" is not cleaned after the function returns. I thought const char is interpreted same way as const char* but it is not true as it seems.
p is a local variable, which you return by value, but points to a <strong>string literal</strong>, which resides in read-only memory, not in the automatic memory allocated for the method.
t and the using it indeed results in undefined behavior.
Also, don't think of pointers and arrays to be equivalent.
p is a local variable, what it points to is not local - it is a compile-time string constant; it is legal to return that constant's address from a function.
t is different, because the compile-time string constant is copied into an automatic storage area, causing an undefined behavior on dereferencing the returned pointer.