1

Considering the following code :

#include <vector>
#include <iostream>
#include <array>

using namespace std;
int main(){

int* ptr;
ptr = new int(3);

int* stock = *&*&ptr;


ptr = nullptr;
cout << *stock<< endl;

cout << *ptr << endl;


}

that declares a pointer that allocates dynamic memory.
when another var stocks the adress of the memory.
Then, the pointer is deleted but the memory is not erased.

You can still access the memory using the copy of the pointer.
If you then try to access the memory using the pointer, the programs crashes.

I would like to know, if it is possible, knowing that the program will crash, to make a function that : in case of crash, stop the program from closing, and just cout << "some message".


thank you

ps: i know that crash means "unrecoverable error", so my question is more a creative nature.

assayag.org
  • 709
  • 10
  • 24
  • Technically, every case, where crash _may_ occur, it is undefined behavior, so _crash_ is just one of the possibilities of what _may_ happen. – Algirdas Preidžius Jan 18 '17 at 01:15
  • You have `null'd` the `ptr`, but not `free'd` the memory. This is not a crashable event, just a logic error. Garbage in, garbage out. – AJNeufeld Jan 18 '17 at 01:15
  • this line does not do what you think it does: `int* stock = *&ptr;` – anthony sottile Jan 18 '17 at 01:15
  • I'm thinking that there is no way to do this, short of using some system calls. – Hot Licks Jan 18 '17 at 01:25
  • _"i know that crash means 'unrecoverable error', so my question is more a creative nature."_ I'm not following this. You already know you can't do this, so what sort of creativity do you seek? Magic? – Lightness Races in Orbit Jan 18 '17 at 01:29
  • Thats funny that this forum is called stack overflow and nobody actually brought the idea of Buffer overflow. I mean, buffer overflow isnt a way to exploit such segmentation fault issues to actually get over a system? – assayag.org Jan 18 '17 at 01:39
  • 1
    @dandecasa We are usually trying to get productive, non-malicious programming work done around here, so no one will suggest you write an overflow or use after free exploit to recover from a crash. That would just be a terrible idea. – Baum mit Augen Jan 18 '17 at 01:48
  • Well, i just ask this for learning purposes. My question is only to understand the type of procedures that would allow this kind of behavior. I think this can be a diverse and interesting subject and not a duplicate question. – assayag.org Jan 18 '17 at 01:51
  • Your edit should push your question in the reopen queue, so the community will review my closevote. I'm confident they will agree though. – Baum mit Augen Jan 18 '17 at 01:57

0 Answers0