fork(1) download
  1. #include <map>
  2. #include <iostream>
  3. #include <memory>
  4. #include <limits>
  5.  
  6.  
  7. template<typename T>
  8. class MyAllocator{
  9. public :
  10. // typedefs
  11.  
  12. typedef T value_type;
  13. typedef value_type* pointer;
  14. typedef const value_type* const_pointer;
  15. typedef value_type& reference;
  16. typedef const value_type& const_reference;
  17. typedef std::size_t size_type;
  18. typedef std::ptrdiff_t difference_type;
  19.  
  20. public :
  21. // convert an allocator<T> to allocator<U>
  22.  
  23. template<typename U>
  24. struct rebind {
  25. typedef MyAllocator<U> other;
  26. };
  27.  
  28. public :
  29. MyAllocator() {}
  30.  
  31. template<typename U>
  32. MyAllocator(MyAllocator<U> const&) {}
  33.  
  34. // address
  35.  
  36. pointer address(reference r) { return &r; }
  37. const_pointer address(const_reference r) { return &r; }
  38.  
  39. // memory allocation
  40.  
  41. pointer allocate(size_type cnt,
  42. typename std::allocator<void>::const_pointer = 0) {
  43. std::cout<<"Trying to allocate "<<cnt<<" objects in memory"<<std::endl;
  44. pointer new_memory = reinterpret_cast<pointer>(::operator new(cnt * sizeof (T)));
  45. std::cout<<"Allocated "<<cnt<<" objects in memory at location:"<<new_memory<<std::endl;
  46. return new_memory;
  47. }
  48. void deallocate(pointer p, size_type n) {
  49. ::operator delete(p);
  50. std::cout<<"Deleted "<<n<<" objects from memory"<<std::endl;
  51. }
  52. // size
  53. size_type max_size() const {
  54. return std::numeric_limits<size_type>::max() / sizeof(T);
  55. }
  56.  
  57. // construction/destruction
  58.  
  59. void construct(pointer p, const T& t) {
  60. std::cout<<"Constructing at memory location:" <<p<<std::endl;
  61. new(p) T(t);
  62. }
  63. void destroy(pointer p) {
  64. std::cout<<"Destroying object at memory location:" <<p<<std::endl;
  65. p->~T();
  66. }
  67.  
  68. bool operator==(MyAllocator const&) { return true; }
  69. bool operator!=(MyAllocator const& a) { return !operator==(a); }
  70. }; // end of class MyAllocator
  71.  
  72. template<typename Key, typename T>
  73. using Map = std::map<Key, T, std::less<Key>, MyAllocator<std::pair<const Key, T>>>;
  74.  
  75. int main() {
  76.  
  77. typedef Map<int, int> Inner;
  78. typedef Map<int, Inner> Outer;
  79. Inner m_inner;
  80. Outer m_outer;
  81. m_inner[0] = 1234;
  82. m_inner[1] = 1234;
  83. m_inner[2] = 1234;
  84. m_outer[1] = m_inner;
  85. std::cout << "hahaha " << m_outer[1][0] << std::endl;
  86.  
  87. return 0;
  88. }
Success #stdin #stdout 0s 3480KB
stdin
Standard input is empty
stdout
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x9129008
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x9129028
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x9129048
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x9129068
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x9129098
Constructing at memory location:0x9129098
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x91290b8
Constructing at memory location:0x91290b8
Trying to allocate 1 objects in memory
Allocated 1 objects in memory at location:0x91290d8
Constructing at memory location:0x91290d8
hahaha 1234
Destroying object at memory location:0x9129068
Destroying object at memory location:0x91290b8
Deleted 1 objects from memory
Destroying object at memory location:0x9129098
Deleted 1 objects from memory
Destroying object at memory location:0x91290d8
Deleted 1 objects from memory
Deleted 1 objects from memory
Destroying object at memory location:0x9129048
Deleted 1 objects from memory
Destroying object at memory location:0x9129028
Deleted 1 objects from memory
Destroying object at memory location:0x9129008
Deleted 1 objects from memory