bhupixb

Overloading new operator and not using Free

Jan 7th, 2020
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. char myHeap[1 << 20];
  4. int sz = 0;
  5.  
  6. void* operator new ( std::size_t count ) {
  7. std::cout << "Operator new is called, count = " << count << std::endl;
  8. sz += count;
  9. return myHeap + sz - count;
  10. }
  11. // it's necessary as call to delete will be made to deallocate memory
  12. // allocated by new, since we allocated memory from array we need not
  13. // to free it in delete function since the array is going to destroy itself
  14. // void operator delete (void *ptr) {}
  15. void operator delete (void *ptr) {
  16. std::cout << "Operator delete is called" << std::endl;
  17. }
  18.  
  19. std::string s;
  20. int main() {
  21. for (char c = 'a'; c <= 'z'; c++) {
  22. std::cout << "Adding char '" << c << "' to string" << std::endl;
  23. s.push_back(c);
  24. }
  25. // own *p = new own();
  26. return 0;
  27. }
Add Comment
Please, Sign In to add comment