-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmyNew4.hpp
56 lines (40 loc) · 1.13 KB
/
myNew4.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
55
56
#ifndef MY_NEW4
#define MY_NEW4
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <new>
#include <array>
int const MY_SIZE = 10;
int counter = 0;
std::array<void* , MY_SIZE> myAlloc{nullptr, };
void * newImpl(std::size_t sz, char const * file, int line){
void * ptr = std::malloc(sz);
std::cerr << file << ": " << line << " " << ptr << '\n';
myAlloc.at(counter++) = 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_NEW4