-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmyNew5.hpp
54 lines (40 loc) · 1.11 KB
/
myNew5.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#ifndef MY_NEW5
#define MY_NEW5
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <new>
#include <string>
#include <vector>
std::vector<void*> myAlloc;
void * newImpl(std::size_t sz, char const * file, int line){
static int counter{};
void * ptr = std::malloc(sz);
std::cerr << file << ": " << line << " " << ptr << '\n';
myAlloc.push_back(ptr);
return ptr;
}
void * operator new(std::size_t sz, char const * file, int line){
return newImpl(sz, file, line);
}
void * operator new [](std::size_t sz, char const * file, int line){
return newImpl(sz, file, line);
}
void operator delete(void * ptr) noexcept{
auto ind = std::distance(myAlloc.begin(), std::find(myAlloc.begin(), myAlloc.end(), ptr));
myAlloc[ind] = nullptr;
std::free(ptr);
}
#define new new(__FILE__, __LINE__)
void dummyFunction(){
int * dummy = new int;
}
void getInfo(){
std::cout << '\n';
std::cout << "Allocation: " << '\n';
for (auto i: myAlloc){
if (i != nullptr) std::cout << " " << i << '\n';
}
std::cout << '\n';
}
#endif // MY_NEW5