Skip to content

Instantly share code, notes, and snippets.

@dmikurube
Created May 16, 2012 06:15
Show Gist options
  • Save dmikurube/2707984 to your computer and use it in GitHub Desktop.
Save dmikurube/2707984 to your computer and use it in GitHub Desktop.
/*
* stdout compiled without -fallocated-type-in-operator-new:
* > 0
* > 2
* > 9
* > 5
*
* stdout compiled without -fallocated-type-in-operator-new:
* > Allocated at 0xb80010 as an instance of 5Klass
* > 0
* > Allocated at 0xb80010 as an instance of 5Klass
* > 2
* > Allocated at 0xb80030 as an instance of 6Klass2
* > 9
* > Allocated at 0xb80050 as an instance of i
* > 5
* > Allocated at 0xb80070 as an instance of Pi
*/
#include <iostream>
#include <typeinfo>
void* operator new(size_t size, const std::type_info &t) {
void* address = operator new(size);
std::cout << "Allocated at " << address << " as an instance of " << t.name() << std::endl;
return address;
}
class Klass {
public:
Klass() : x_(0) {}
Klass(int x) : x_(x) {}
void dump() { std::cout << x_ << std::endl; }
private:
int x_;
};
class Klass2 {
public:
int x;
};
int main() {
Klass* k;
k = new Klass();
k->dump();
delete k;
k = new Klass(2);
k->dump();
Klass2* k2;
k2 = new Klass2();
k2->x = 9;
std::cout << k2->x << std::endl;
int *i;
i = new int(5);
std::cout << *i << std::endl;
int **pi;
pi = new int*();
delete k;
delete k2;
delete i;
delete pi;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment