J's Study log

[C++] new & delete Operator Overloading 본문

Computer Language/C++

[C++] new & delete Operator Overloading

정우섭 2020. 6. 27. 00:11

new operator

 

There are three different of representative function of new operator.

 

  1. Memory Allocation
  2. Calling Constructor
  3. Type casting of returned address to appropriate type.

 

Overloading new operator actually means not the overloading every function of new operator, you just change how to allocate the memory.

 

In other words, the way of calling a constructor and type casting of returned address cannot be overloaded.

 

void* operator new(size_t size) {...}

 

This is the form of overloading 'new operator', return type must be 'void*' type and type of parameter must be 'size_t'.

 

Make sure that the return value of new overloading function is 'the address of allocated memory space'.

 

Next the compiler will call constructor and reset the allocated memory space.

 

And finally returns the constructed object's address after casting it to the class pointer.

 

The point is that the return value of new operator is not the return value of overloaded new function.

 

 

 

delete operator

 

Point* ptr = new Point(3, 4);	//Point is a class
delete ptr;

 

When destructing an object the compiler first calls the destructor of the object which 'ptr' is pointing.

 

And it passes the address value of ptr to the function like this.

 

void operator delete(void* adr) {...}	//The form of delete function

 

Overloaded delete function have responsible of destruction of the allocated memory space.

(Of course you can add more things too)

 

void operator delete (void* adr)
{
	delete []adr;
}

 

If the compiler doesn't allow to operate delete for void* type, use type casting like this code.

 

void operator delete (void* adr)
{
	delete []((char*)adr);
}

 

 

 

operator new[]

If you allocate array just put [ ] after overloaded function.

 

void* operator new[](size_t size) {...}
void* operator delete[](void* adr) {...}

 

delete []arr;	//how to call overloaded delete[] function

When this code is compiled, the compiler calls destructor first.

 

And it passes the address value of 'arr' to function 'void operator delete[] (void* adr){...}' while calling it.

 

 

 

Example code

#include <iostream>
using namespace std;

class Point
{
private:
	int xpos, ypos;
public:
	Point(int x = 0, int y = 0) : xpos(x), ypos(y) {  }
	friend ostream& operator<<(ostream& os, const Point& pos);

	void * operator new (size_t size)
	{
		cout << "operator new : " << size << endl;
		void * adr = new char[size];
		return adr;
	}
	void * operator new[](size_t size)
	{
		cout << "operator new [] : " << size << endl;
		void * adr = new char[size];
		return adr;
	}

	void operator delete (void * adr)
	{
		cout << "operator delete ()" << endl;
		delete[]adr;
	}
	void operator delete[](void * adr)
	{
		cout << "operator delete[] ()" << endl;
		delete[]adr;
	}
};

ostream& operator<<(ostream& os, const Point& pos)
{
	os << '[' << pos.xpos << ", " << pos.ypos << ']' << endl;
	return os;
}

int main(void)
{
	Point * ptr = new Point(3, 4);
	Point * arr = new Point[3];

	delete ptr;
	delete[]arr;
	return 0;
}

 

 

 

 

 

 

 

 

 

[참고자료]

- 윤성우의 열혈 C++ 프로그래밍

Comments