I have question about new placement of array in c++. below code is a sample code that i made.
#include <vector>
#include <iostream>
class Point
{
int x,y;
public:
Point():x(0), y(0){std::cout<<"Point() : "<<this<<std::endl;}
void print(){std::cout<<x<<":"<<y<<std::endl;}
Point(int a, int b) :x(a), y(b){std::cout<<"Point(int,int) : value & addr "<<a<<":"<<b<<" ~ "<<this<<std::endl;}
~Point(){std::cout<<"~Point() : "<<this<<" "<<x<<":"<<y<<std::endl;}
};
int main()
{
// multiple allocation
void* mem_ptr_arr = operator new(sizeof(Point)*3);
for(int i=0; i<3; i++)
new( mem_ptr_arr+sizeof(Point)*i ) Point(i,i);
Point* ref_ptr_arr = static_cast<Point*>(mem_ptr_arr);
// delete process
for(int i=0; i<3; i++)
(ref_ptr_arr+i)->~Point();
operator delete(ref_ptr_arr);
Point* new_ptr = new Point[3]{};
delete[] new_ptr;
return 0;
}
I want to duplicate functionality of new and delete operation. So break down each operation like below
- new -> operator new + new(some_ptr) Constructor
- delete -> Obj.~Destructor + delete(some_ptr) My question is, Is it correct usage of new placement of array(ref_ptr_arr)? When i debug some memory, they do not use same heap address after delete previous pointer.