New, operator new, placement new

Source: Internet
Author: User
A new (new operator) and operator new Object Dynamic creation method: New T; The essence is a two-stage operation: 1 uses operator new for memory allocation: void* operator new (size_t size). 2 invokes the corresponding constructor for construction. We can rewrite the global operator new to make our own memory allocations, which can be achieved through the malloc function
Create time:2014-7-29 09:14:39 by Ldlan
///C + +, VS2010
#include <iostream>
using namespace std;
  //overwrites the global operator new function for memory allocation
void* operator new (size_t size)
{
	void* p = malloc (size);
	return p;
}

Class time
{
private:
	int, month, day;
Public: Time
	(): Year (1970), month (1), day (1) {}
};

int main (void)
{
	//will be invoked to the custom operator new function time
	*today = new Time;
	
	Delete today;
	return 0;
}
It's also very necessary to overwrite the operator delete
void operator delete (void *p)
{free
	(p);
}
Use delete p; instead of new T; (1) The object destructor (class type), (2) is invoked after the operator delete, and the memory is freed.
You can also make operator new a member function of a class, so that when you create an object in new, it is automatically recognized by the object (this) and calls the operator new function for memory allocation, as follows
Class time
{
private:
	int, month, day;
Public: Time
	(1970), month (1), day (1) {}
	~time () {
	}	
	//To define operator new as a member function
	void* New (size_t size)
	{
		void* p = malloc (size);
		return p;
	}
	Defines the operator delete as a member function
	void operator delete (void *p)
	{free
		(p);
	}
};
Sometimes even when allocating memory in a class member function is still just a way of using external global allocations, we can call the global memory allocation function:
Class time
{
private:
	int, month, day;
Public: "Time
	" (1970), month (1), day (1) {}
	~time () {
	}
	void* operator new (size_t size)
	{
		//Use global allocation function in member memory allocation function return
		:: operator new (size);
	}
	void operator delete (void *p)
	{
		//Use global release function in member memory release function
		:: operator delete (p);
	}
;
Summary: 1 new T calls operator new for memory allocation, followed by the construction of the object. 2 cannot overwrite the new overload, but it can overwrite the global operator new for custom memory allocations, or define and assign functions to members, and it is necessary to overwrite the operator delete.
The method of two  placement new calls is: New (p) T; P is the allocated memory, T for the type to be constructed by the object, the object is constructed at p memory, and the type is T. The essence is that it has only one operation, that is, the object construct in the memory referred to in P, the object constructor T () will be invoked. The <new> header file provides a way to construct an object in a given memory this is: Placement new. This is an overload of operator new, a global function that cannot be overwritten or otherwise treated as a redefinition. The following code example customizes the memory allocation function and invokes placement new to construct its own object, in fact, it replaces the two steps of the new creation of the object
///Create time:2014-7-29 09:14:39 by Ldlan///C + +, VS2010 #include <iostream> using namespace St

D
	Custom memory allocation function void* operator new (size_t size) {void* p = malloc (size);
return p;

}//Custom memory release function void operator delete (void *p) {free (p);} Class Time {Private:int year, month, Day Public:time (): Year (1970), month (1), day (1) {} ~time () {}//member memory allocation function, internal call global
	The memory allocation function is, this is also our custom, the global memory allocation function has been overridden by US overwrite void* operator new (size_t size) {return:: operator new (size);
	}//member memory release function void operator delete (void *p) {:: operator delete (p);


}
}; int main (void) {//The following two steps is the new Create object's replacement//(1) Call member memory allocation function, and actually call to the global memory allocation function we rewritten//is void* today = malloc (sizeof)
	);

	void *today = operator new (sizeof (time));  (2) Call in placement new, in the memory where today refers to the object construction, that is, call the constructor time ():: New [Today] time;
	:: In order to invoke the global placement new//More perfect here should call the object's destructor, operator delete only memory release operator delete (today);
return 0; }
If an attempt to define placement new as a class member is actually an overload of the member operator new, it is a different two function from the global placement and does not implement the object's construction process.
Create time:2014-7-29 09:14:39 by Ldlan
///C + +, VS2010
#include <iostream>

using namespace std;
  void* operator new (size_t size)
{
	void* p = malloc (size);
	return p;
}
void operator delete (void *p)
{free
	(p);
}


Class time
{
private:
	int, month, day;
Public: "Time
	" (1970), month (1), day (1) {}
	~time () {
	}
	void* operator new (size_t size)
	{ Return
		:: operator new (size);
	}
	void operator delete (void *p)
	{
		:: operator delete (p);
	}
	The expected definition member placement new, which is actually the overload of the member operator new, does not have an object construct
	void* operator new (size_t size, void *p)
	{
		return p;
	}
;


int main (void)
{
	//memory allocation
	void *today = operator new (sizeof (time));
	Object construct new (today) time is missing
	;  No::

	operator Delete (today);
	return 0;
}
Summary: 1 placement New specifically constructs the object in the specified memory, the global placement new:void* operator new (size_t size, void *p) can not be overwritten, calling the function will cause calls to the constructor, to ensure that P A memory allocation was made.
Three can be overloaded to operator new form
void* operator new (size_t size, T *p, ...)
void* operator Delete (void*, T *p, ...)
Wait a minute.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.