-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmyNew2.hpp
41 lines (30 loc) · 799 Bytes
/
myNew2.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
#ifndef MY_NEW2
#define MY_NEW2
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <new>
#include <string>
#include <array>
int const MY_SIZE = 10;
std::array<void * , MY_SIZE> myAlloc{nullptr, };
void * operator new(std::size_t sz){
static int counter{};
void * ptr = std::malloc(sz);
myAlloc.at(counter++) = ptr;
return ptr;
}
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);
}
void getInfo(){
std::cout << '\n';
std::cout << "Not deallocated: " << '\n';
for (auto i: myAlloc){
if (i != nullptr) std::cout << " " << i << '\n';
}
std::cout << '\n';
}
#endif // MY_NEW2