Hi all,
I've got an apparently trivial issue. Consider the following snippet (compiled with gcc 4 on ubuntu breezer):
I expect the "cout << c" instruction to cause a run-time error, as string::c_str() does not allocate memory but returns a pointer to the string instance's internal data. This data should be destroyed along with s by the time f() terminates.Code:const char* f() {
string s("foo bar");
return s.c_str();
}
int main() {
const char* c = f();
cout << c << endl; //shouldn't it crash?
}
I tried running the program through valgrind and still I got no errors. Eventually I tried on windows (vc++ 7.1) and rational purify finally reported an error.
Any ideas as to why everything runs smoothly on linux? I know this can be due to freed blocks not being returned immediately etc. but is this not an incident waiting to happen?
Thanks in advance
