| 1 | #include <new> |
| 2 | |
| 3 | struct new_tag_t |
| 4 | { |
| 5 | }; |
| 6 | new_tag_t new_tag; |
| 7 | |
| 8 | struct Struct { |
| 9 | int value; |
| 10 | }; |
| 11 | |
| 12 | bool operator==(const Struct &a, const Struct &b) { |
| 13 | return a.value == b.value; |
| 14 | } |
| 15 | |
| 16 | typedef char buf_t[sizeof(Struct)]; |
| 17 | buf_t global_new_buf, tagged_new_buf; |
| 18 | |
| 19 | // This overrides global operator new |
| 20 | // This function and the following does not actually allocate memory. We are merely |
| 21 | // trying to make sure it is getting called. |
| 22 | void * |
| 23 | operator new(std::size_t count) |
| 24 | { |
| 25 | return &global_new_buf; |
| 26 | } |
| 27 | |
| 28 | // A custom allocator |
| 29 | void * |
| 30 | operator new(std::size_t count, const new_tag_t &) |
| 31 | { |
| 32 | return &tagged_new_buf; |
| 33 | } |
| 34 | |
| 35 | int main() { |
| 36 | Struct s1, s2, s3; |
| 37 | s1.value = 3; |
| 38 | s2.value = 5; |
| 39 | s3.value = 3; |
| 40 | return 0; // break here |
| 41 | } |
| 42 | |
| 43 | |