-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path16_policy_base2.cpp
More file actions
44 lines (38 loc) · 817 Bytes
/
16_policy_base2.cpp
File metadata and controls
44 lines (38 loc) · 817 Bytes
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
#include <vector>
template <typename T, typename Alloc = std::allocator<T>>
class vector
{
alloc ax;
public:
void resize(int sz)
{
T *buf = ax.allocate(sz);
// maybe. need to copy data.
ax.deallocate(buf, size);
}
};
template <typename T>
struct debug_alloc
{
T *allocate(std::size_t size)
{
T *ptr = static_cast<T *>(malloc(sizeof(T) * size));
printf("%d allocated at %p\n", size, ptr);
return ptr;
}
void deallocate(T *ptr, int size)
{
free(ptr);
printf("%d deallocated at %p\n", size, ptr);
}
debug_alloc() {}
using value_type = T;
template <typename U>
debug_alloc(const U &) {}
};
int main()
{
std::vector<int> v(5);
std::vector<int, debug_alloc<int>> v(5);
v.resize(10);
}