-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsample.cpp
More file actions
61 lines (48 loc) · 1.76 KB
/
Copy pathsample.cpp
File metadata and controls
61 lines (48 loc) · 1.76 KB
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
57
58
59
60
61
#include <iostream>
#include <memory>
// Custom allocator
template <class T>
struct MyAllocator {
using value_type = T;
MyAllocator() noexcept = default;
template <class U>
MyAllocator(const MyAllocator<U>&) noexcept {}
T* allocate(std::size_t n) {
std::cout << "Custom allocation of " << n << " objects\n";
return static_cast<T*>(::operator new(n * sizeof(T)));
}
void deallocate(T* p, std::size_t n) noexcept {
std::cout << "Custom deallocation of " << n << " objects\n";
::operator delete(p);
}
};
int main() {
// Using std::allocate
{
std::allocator<int> alloc;
int* ptr = alloc.allocate(5); // Allocating memory for 5 ints
alloc.deallocate(ptr, 5); // Deallocating the memory
}
std::cout << "-----------------\n";
// Using custom allocator
{
MyAllocator<int> alloc;
int* ptr = alloc.allocate(5); // Allocating memory for 5 ints using custom allocator
alloc.deallocate(ptr, 5); // Deallocating the memory using custom allocator
}
std::cout << "-----------------\n";
// Using std::allocate_shared
{
std::allocator<int> alloc;
auto ptr = std::allocate_shared<int>(alloc, 42); // Allocating and constructing an int using std::allocate_shared
std::cout << *ptr << '\n'; // Accessing the value
}
std::cout << "-----------------\n";
// Using custom allocator with std::allocate_shared
{
MyAllocator<int> alloc;
auto ptr = std::allocate_shared<int>(alloc, 42); // Allocating and constructing an int using custom allocator with std::allocate_shared
std::cout << *ptr << '\n'; // Accessing the value
}
return 0;
}