Created
January 21, 2021 08:36
-
-
Save raggleton/e063131090f4e7adb95a6eda55b03a46 to your computer and use it in GitHub Desktop.
Testing raw pointer deletion. Compile with `g++ -o demo -g -O0 -Wall pointer_demo.cc `
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
using namespace std; | |
class A { | |
public: | |
A(): thing(nullptr) {} | |
void DoThing(int * x) { | |
if (x != thing) { | |
delete thing; | |
thing = x; | |
} | |
} | |
int * thing; | |
}; | |
int main() { | |
cout << "running demo" << endl; | |
A a; | |
cout << "a.thing: " << a.thing << endl; | |
int * xxx = new int(3); | |
a.DoThing(xxx); | |
cout << "a.thing: " << a.thing << " : " << *(a.thing) << endl; | |
cout << "xxx: " << xxx << " : " << *xxx << endl; | |
int * yyy = new int(4); | |
a.DoThing(yyy); | |
cout << "a.thing: " << a.thing << " : " << *(a.thing) << endl; | |
cout << "xxx: " << xxx << " : " << *xxx << endl; | |
cout << "yyy: " << yyy << " : " << *yyy << endl; | |
cout << "Trying to delete xxx:" << endl; | |
if (xxx != NULL) { | |
cout << "xxx is not NULL" << endl; | |
delete xxx; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment