v***@gmail.com
2006-05-21 03:42:26 UTC
Hi,
I have a question on the following code:
void foo(string p) {
const char *cptr;
{
string str = "Hello" + p;
cptr = str.c_str();
}
// does cptr at this line points to safe memory?
}
I understand that the pointer value returned by c_str() points to the
string object's internal array. In this case, the string object "str"
is allocated on the stack so I believe its internal array would also be
on the stack which would go out of scope after "cptr = str.c_str();"
Is it correct that cptr will point to bad memory?
Thanks
I have a question on the following code:
void foo(string p) {
const char *cptr;
{
string str = "Hello" + p;
cptr = str.c_str();
}
// does cptr at this line points to safe memory?
}
I understand that the pointer value returned by c_str() points to the
string object's internal array. In this case, the string object "str"
is allocated on the stack so I believe its internal array would also be
on the stack which would go out of scope after "cptr = str.c_str();"
Is it correct that cptr will point to bad memory?
Thanks