Imagine such code:
string f()
{
string r = "ab";
return r;
}
int main() {
const char *c = f().c_str();
printf("%s.\n", c);
return 0;
}
This code may crash, right? Because that string that c
points to is destroyed. But running it via Valgrind doesn't show any invalid memory accesses. Why? I know Valgrind cannot check the stack, but "ab" actually is located on the heap, right?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With