Created
June 20, 2020 18:04
-
-
Save javedbaloch4/903b7c25289d9b359497d31c024c3064 to your computer and use it in GitHub Desktop.
C++ Without overloading delete operator, make an object of any of the above classes and delete it without calling destructor or using free()
This file contains hidden or 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> | |
| using namespace std; | |
| class A { | |
| private: | |
| public: | |
| int a = 10; | |
| A() { | |
| cout << "A's Constructor is called!" << endl; | |
| } | |
| }; | |
| void ptr_Deallocate(A* ptr_A) { | |
| delete[] ptr_A; | |
| } | |
| int main() { | |
| int size = 5; | |
| A* ptr_A = new A[size]; | |
| ptr_Deallocate(ptr_A); | |
| cout << ptr_A->a; //-- Check for proper deallocation. | |
| ptr_A = NULL; | |
| //-- Check for dangling pointer. | |
| if (ptr_A == NULL) { | |
| cout << "Pointer A is null!" << endl; | |
| } | |
| else { | |
| cout << "Pointer A is not null!" << endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment