-1

string pointer pp is temporary, why is it still correct to cout the c_str after I delete the pointer?

#include <string>
#include <iostream>
using namespace std;
int main(){
    const char* tt = NULL;
    {   
    string * pp = new string("big");
    tt = pp->c_str();
    cout << "tt->pp:\t" << tt << endl;
    delete pp; 
    }   
    cout << tt << endl;
    return 0;
}

The output is:

tt->pp: big
big
Ridox
  • 1,073
  • 13
  • 18
  • 1
    undefined behavior /thread. – Luchian Grigore Dec 09 '14 at 15:19
  • The mentioned *duplicate* isn't one, because it is about accessing *local variables*. This question is about accessing freed memory. – Olaf Dietsche Dec 09 '14 at 16:02
  • @OlafDietsche: Local variables out of scope are effectively freed memory. The duplicate is correct and, if you read it, you'll draw towards understanding why that is. – Lightness Races in Orbit Dec 09 '14 at 16:45
  • @LightnessRacesinOrbit Not at all, maybe I should have been more detailed for those playing dumb. The error here is accessing freed *heap* memory. – Olaf Dietsche Dec 09 '14 at 17:00
  • @OlafDietsche: How does it matter which physical location was chosen to store the `std::string` data? It's gone out of scope. The OP is asking why he can still read it instead of getting a segmentation fault or other error. It's the same thing. – Lightness Races in Orbit Dec 09 '14 at 17:01
  • @LightnessRacesinOrbit It does matter, since the memory of local variables as in the linked question is overwritten, when you call another function. Heap memory is overwritten, when you do another heap allocation. So getting a segmentation fault is more likely in the first case, accessing out of scope local variables. Accessing freed heap memory might be untouched and thus accessible unpunished a lot longer. Anyway, I think, you're well aware of these differences, at least your reputation and badges suggest so. :-) – Olaf Dietsche Dec 09 '14 at 17:13
  • @OlafDietsche: Yes, indeed. Apparently my reputation and badges don't go as far as to persuade you to take what I'm saying seriously, though! – Lightness Races in Orbit Dec 09 '14 at 17:13

1 Answers1

-1

It's not correct. At all.​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • I know it is bad-formed. I ask why the program outputs successfully when I expect an error or segmentation fault. Is the c_str in the heap or on stack? – Ridox Dec 09 '14 at 15:37
  • 2
    @ridox: It doesn't matter where your implementation put it while it existed. It doesn't exist now. You assume that accessing invalid memory will always give you an error or segmentation fault but this is absolutely not true. – Lightness Races in Orbit Dec 09 '14 at 15:41
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post. – SiKing Dec 09 '14 at 16:20
  • @SiKing: **Q:** _"why is it still correct to cout the c_str after I delete the pointer?"_ **A:** _"It isn't"_. End of story. This is clearly neither a critique nor a request for clarification. – Lightness Races in Orbit Dec 09 '14 at 16:45
  • The system flagged this as a potential low quality answer. I agreed. If that is indeed the answer, then it sounds as if the OP should have been flagged to close instead? – SiKing Dec 09 '14 at 16:55
  • @SiKing: Then you shouldn't have chosen this comment to add to the answer, as it obviously does not fit. – Lightness Races in Orbit Dec 09 '14 at 17:01