Advertisement
Guest User

Untitled

a guest
Jul 12th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.53 KB | None | 0 0
  1. #include <memory>
  2.  
  3. struct Toto {
  4.   int a;
  5.   static void* operator new(size_t n)
  6.   {
  7.     return malloc(n);
  8.   }
  9.  
  10.   static void operator delete(void* ptr, size_t)
  11.   {
  12.     return free(ptr);
  13.   }
  14. };
  15.  
  16. int main(int argc, char** argv) {
  17.   std::unique_ptr<Toto> buf(new Toto{argc});
  18.   if (!buf) {
  19.     return 1;
  20.   }
  21.   printf("%d\n", buf->a);
  22.  
  23.  
  24.   std::unique_ptr<int, decltype(free)*> MyInt((int*)malloc(sizeof(int)), free);
  25.   if (!MyInt) {
  26.     return 1;
  27.   }
  28.   *MyInt = argc+10;
  29.   printf("%d\n", *MyInt);
  30.   return 0;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement