Last active
December 11, 2015 03:09
-
-
Save dmikurube/4535834 to your computer and use it in GitHub Desktop.
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
// RUN: clang++ -fintercept-allocation typeaware.cpp | |
#include <stdio.h> | |
#include <typeinfo> | |
#include <iostream> | |
struct Klass { | |
int x, y, z; | |
Klass() { x = 1; y = 2; z = 3; } | |
~Klass() { x = 0; } | |
}; | |
struct VirtualKlass { | |
char a, b, c; | |
VirtualKlass() { a = 3; b = 10; c = 20; } | |
virtual ~VirtualKlass() { b = 9; } | |
}; | |
void* __intercept_new__( | |
void* address, const std::type_info& type, std::size_t size) { | |
printf("Allocated %lu bytes for %s at %016p.\n", | |
size, type.name(), address); | |
return address; | |
} | |
void* __intercept_delete__( | |
void* address, const std::type_info& type, std::size_t size) { | |
printf("Deallocated %lu bytes for %s at %016p.\n", | |
size, type.name(), address); | |
return address; | |
} | |
void* __intercept_delete__( | |
void* address, const std::type_info& type) { | |
printf("Deallocated %s at %016p.\n", | |
type.name(), address); | |
return address; | |
} | |
int main() { | |
int *i; | |
char *s; | |
Klass *k; | |
VirtualKlass *v; | |
i = new int(3); | |
s = new char[12]; | |
k = new Klass(); | |
v = new VirtualKlass(); | |
delete v; | |
delete k; | |
delete[] s; | |
delete i; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output should be (addresses changes run-by-run):
Allocated 4 bytes for i at 0x000000006ce010.
Allocated 12 bytes for c at 0x000000006ce030.
Allocated 12 bytes for 5Klass at 0x000000006ce050.
Allocated 16 bytes for 12VirtualKlass at 0x000000006ce070.
Deallocated 12VirtualKlass at 0x000000006ce070.
Deallocated 5Klass at 0x000000006ce050.
Deallocated c at 0x000000006ce030.
Deallocated i at 0x000000006ce010.