|
-
February 23rd, 2006, 10:26 AM
#1
Memory mgmt issue
Hi all,
I've got an apparently trivial issue. Consider the following snippet (compiled with gcc 4 on ubuntu breezer):
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 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.
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|