placement new

Operator new

operator new 是一个函数,只是单纯的用来分配内存,类似于malloc, 其函数形式形如下式。

Operator new is a function that allocates raw memory and conceptually a bit similar to malloc().

  • It is the mechanism of overriding the default heap allocation logic.
  • It doesn’t initializes the memory i.e constructor is not called. However, after our overloaded new returns, the compiler then automatically calls the constructor also as applicable.
  • It’s also possible to overload operator new either globally, or for a specific class

void * operatpr new (size_t);
返回一个指针,指向一块原始的为初始化的指针。

  1. // CPP program to illustrate
  2. // use of operator new
  3. #include<iostream>
  4. #include<stdlib.h>
  5. using namespace std;
  6. class car
  7. {
  8. string name;
  9. int num;
  10. public:
  11. car(string a, int n)
  12. {
  13. cout << "Constructor called" << endl;
  14. this->name = a;
  15. this->num = n;
  16. }
  17. void display()
  18. {
  19. cout << "Name: " << name << endl;
  20. cout << "Num: " << num << endl;
  21. }
  22. void *operator new(size_t size)
  23. {
  24. cout << "new operator overloaded" << endl;
  25. void *p = malloc(size);
  26. return p;
  27. }
  28. void operator delete(void *ptr)
  29. {
  30. cout << "delete operator overloaded" << endl;
  31. free(ptr);
  32. }
  33. };
  34. int main()
  35. {
  36. car *p = new car("HYUNDAI", 2012);
  37. p->display();
  38. delete p;
  39. }
  40. //Output
  41. new operator overloaded
  42. Constructor called
  43. Name:HYUNDAI
  44. Num:2012
  45. delete operator overloaded

new Opeartor

new operator是一个关键词,它会执行两个步骤,第一个步骤是分配内存,第二个步骤是调用构造函数来初始化这段内存.由于是内建的,因此无法改变其行为

The new operator is an operator which denotes a request for memory allocation on the Heap. If sufficient memory is available, new operator initializes the memory and returns the address of the newly allocated and initialized memory to the pointer variable. When you create an object of class using new keyword(normal new).

  • The memory for the object is allocated using operator new from heap.
  • The constructor of the class is invoked to properly initialize this memory.
  1. // CPP program to illustrate
  2. // use of new keyword
  3. #include<iostream>
  4. using namespace std;
  5. class car
  6. {
  7. string name;
  8. int num;
  9. public:
  10. car(string a, int n)
  11. {
  12. cout << "Constructor called" << endl;
  13. this ->name = a;
  14. this ->num = n;
  15. }
  16. void enter()
  17. {
  18. cin>>name;
  19. cin>>num;
  20. }
  21. void display()
  22. {
  23. cout << "Name: " << name << endl;
  24. cout << "Num: " << num << endl;
  25. }
  26. };
  27. int main()
  28. {
  29. // Using new keyword
  30. car *p = new car("Honda", 2017);
  31. p->display();
  32. }
  33. //Output
  34. Constructor called
  35. Name: Honda
  36. Num: 2017

new operator vs operator new

  1. Operator vs function: new is an operator as well as a keyword whereas operator new is only a function.
  2. New calls “Operator new”: “new operator” calls “operator new()” , like the way + operator calls operator +()
  3. “Operator new” can be Overloaded: Operator new can be overloaded just like functions allowing us to do customized tasks.
  4. Memory allocation: ‘new expression’ call ‘operator new’ to allocate raw memory, then call constructor.

Reference

https://www.geeksforgeeks.org/new-vs-operator-new-in-cpp/