2015年12月18日 星期五

C++ explicit call of operator new and delete



Below Code is equvalent:
  1.     {
  2.         A * a = new A;
  3.         delete a;
  4.     }
  5.     {
  6.         A * a = (A*)(::operator new(sizeof(A)));
  7.         a->A::A();
  8.         a->~A();
  9.         ::operator delete(a);
  10.     }

  1. #include <iostream>
  2. #include <list>
  3. #include <allocators>
  4. class A
  5. {
  6. public:
  7.     A(){std::cout << " A()\n";}
  8.     ~A(){std::cout << "~A()\n";}
  9. };
  10. int main()
  11. {
  12.     using namespace std;
  13.     {
  14.         A * a = new A;
  15.         delete a;
  16.     }
  17.     {
  18.         A * a = (A*)(::operator new(sizeof(A)));
  19.         a->A::A();
  20.         if (a!=0)
  21.         {
  22.             a->~A();
  23.             ::operator delete(a);
  24.         }
  25.     }
  26.     return 0;
  27. }

沒有留言:

張貼留言

2007 to 2023 HP and Dell Servers Comparison

  HP Gen5 to Gen11  using ChatGPT HP ProLiant Gen Active Years CPU Socket Popular HP CPUs Cores Base Clock Max RAM Capacity Comparable Dell ...