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