Skip to content

Instantly share code, notes, and snippets.

@javedbaloch4
Created June 20, 2020 18:04
Show Gist options
  • Save javedbaloch4/903b7c25289d9b359497d31c024c3064 to your computer and use it in GitHub Desktop.
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()
#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