The difference between operator new and new operator

Source: Internet
Author: User

Original address: http://www.cnblogs.com/jamesmile/archive/2010/04/17/1714311.html, thank you here

operator New in C + + and new operator, look like two sisters, but there is a difference.

operator NEW

(1) Only the required space is allocated, and the constructor of the related object is not called. When the required allocated space is not met, the

-If there is new_handler, call New_handler, otherwise

-If no exception is required (expressed in the nothrow parameter), the BAD_ALLOC exception is executed, otherwise

--Return 0

(2) can be overloaded

(3) When overloading, the return type must be declared as void*

(4) When overloading, the first parameter type must be the size (in bytes) of the allocation space for the expression requirement, type size_t

(5) When overloading, can take other parameters

New operator

(1) Call operator new to allocate enough space and call the constructor of the related object

(2) can not be overloaded

Accordingly, operator delete has similar characteristics to delete operator.

As an example,

class X 
{
public:
…………
    static void* operator new(size_t size)
{
    return ::operator new(size);
}
static void operator delete(void* pointee)
{
    ::operator delete(pointee);
}
…………
};
X* px = new X();

New in the line code is new operator, which invokes operator new in class X, allocates space for the object of the class, and then invokes the constructor of the current instance.

delete px;

The delete in the line code is delete operator, which invokes the destructor for the instance, and then calls the operator delete in class X to free up the space occupied by the instance.

The behavior of new operator and delete operator is not and should not be changed, which is a commitment made by the C + + standard. And operator new and operator delete and the C language of malloc and free correspond, only responsible for allocating and freeing space. However, the space allocated using operator new must be freed using operator delete, not free, because they are not registered in the same way as memory usage. The reverse is also the same.

You can reload operator new and operator delete to implement different requirements for memory management, but you cannot overload the new operator or delete operator to change their behavior.

When you overload operator new, you can provide more arguments, and when you new an object, pass the extra arguments in parentheses after the keyword new. For example, the following classes

class A 
{
public:
    …………
    static void* operator new(size_t size, const string& example)
{
    cout << example << endl;
    return ::operator new(size);
}
…………
};
A* pa = new (“This will be printed out in operator new”) A();

The new standard C + + allows a parameter named Nothrow to be passed in such a way that it does not throw an exception when allocating space to the object, but instead returns 0 to be compatible with the behavior of the old standard new. Like what

class B {};
B* pb = new (nothrow) B();

Of course, this can only be used for classes that use the default operator new operator. For classes that have overloaded operator new (such as The X and a above), you cannot naturally enjoy the gift of the C + + standard If you do not declare that you can accept the Nothrow parameter.

Six overloaded forms of operator new

When writing
p = new P ();
Such code, in fact, there are two steps, the first allocation of memory,
Class members are then initialized on top of the allocated memory.
The second step is to have the constructor complete, and the first step is to work with the new function.
The global new has six overloaded forms,
void *operator New (std::size_t count)
throw (Std::bad_alloc); General version
void *operator New (std::size_t count,//compatible with earlier versions of new
Const std::nothrow_t&) throw (); Memory allocation failure does not throw an exception
void *operator New (std::size_t count, void *ptr) throw ();
Placement version
void *operator new[] (std::size_t count)//
throw (Std::bad_alloc);
void *operator new[] (std::size_t count,//
Const std::nothrow_t&) throw ();
void *operator new[] (std::size_t count, void *ptr) throw ();
So, the usage just now is an overloaded form of using the new function.
If the object of a is overloaded with the same implementation of the new function, as a member function
will be called first.
Introduction to various new C + +
1.new T
The first new is the simplest, call the class (if overloaded) or the global operator new allocates space, and then use the
The arguments to the column following the type to invoke the constructor, and the usage is
New TypeName (initial_args_list). If there are no arguments, the parentheses can generally be omitted. For example
int *p=new int;
int *p=new int (10);
int *p=new foo ("Hello");
To destroy by calling Delete:
Delete p;
2. New t[]
This new is used to create a dynamic array of objects that will call the object's operator new[] to allocate memory (if
No, call operator new, the search order above), and then call the object's default constructor to initialize each object
Usage:
New Typename[num_of_objects];
For example
int *p= new INT[10];
Use operator delete[when destroying]
3.new () T and New () t[]
This is a new with parameters, and this form of new calls operator new (Size_t,othertype) to allocate memory
The Othertype here is compatible with the type of the parameter in the new parenthesis,
This syntax is typically used in a particular address artifact object, called placement new, provided the operator new
(size_t,void*) has been defined, usually the compiler has provided an implementation, including the <new> header file, this
The implementation simply returns the specified address of the parameter, so the new () operator is created on the address in parentheses
Object
It should be explained that the second parameter is not necessarily if void*, can be recognized by the legal type, this time by the overload of C + +
Mechanism to decide to call that operator new
Of course, we can provide our own operator new (Size_,type) to determine the behavior of new, such as
Char data[1000][sizeof (foo)];
Inline void* operator new (size_t, int n)
{
return data[n];
}
You can use this interesting syntax to create an object:
Foo *p=new (6) foo (); To create an object on the sixth cell of data
Yes, it's interesting.
The standard library also provides a nothrow implementation:
void* operator New (std::size_t, const std::nothrow_t&) throw ();
void* operator new[] (std::size_t, const std::nothrow_t&) throw ();
You can implement the call to new failure without throwing an exception
New (nothrow) int (10);
Nothrow is an example of std::nothrow_t
The object created by placement new cannot be destroyed directly by delete, but instead calls the object's destructor to destroy the
Like, as to how the memory of the object is handled, it depends on the specific source of the memory
4. Operator new (size_t)
This operator assigns a parameter to the specified size of memory and returns the first address, which can be overloaded with the operator for a custom class,
The method is to declare in the class plus
void *operator New (size_t size)
{
Allocate memory here and return to its address
}
The various operator new AND operator delete that are overloaded in the class, whether or not declared, are the static properties of the
It is generally not necessary to call operator new directly unless the original memory is allocated directly (this is similar to malloc in C)
In the case of a conflict, call the global operator Plus:: scope operator:
:: operator new (1000); Allocate 1000 bytes
The returned memory needs to be recycled, calling the corresponding operator delete
5.operator new[] (size_t)
This is also the allocation of memory, but is specifically for the array, that is, the new t[] This form, of course, when needed can
Explicitly called
6.operator New (size_t size, Othertype other_value)
And operator new[] (size_t size, Othertype other_value)
See New () above
It should be stressed that new is used to create objects and allocate memory, its behavior is immutable, can change is a variety of
operator new, we can implement our memory allocation scheme by overloading operator new.

The difference between operator new and new operator

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.