Operator new and new operator

Source: Internet
Author: User
Operator new and new operator in C ++ look pretty different from each other. Operator new (1) only allocates the required space and does not call constructors of related objects. When the requested space cannot be allocated,-> If new_handler exists, new_handler is called; otherwise-> if no exception is required ), an exception occurs when bad_alloc is executed. Otherwise, if-> 0 (2) is returned and can be overloaded (3), the return type must be declared as void * (4, the first parameter type must be the size (in bytes) required for the expression. When the type is size_t (5) heavy load, You can include other parameters new operator (1) call operator new to allocate enough space and call the constructor of related objects (2) cannot be reloaded accordingly. operator delete and delete operator have similar features. For 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();

In this line of code, the new is new operator, which calls the operator in class X.
New: allocates space for the objects of this class, and then calls the constructor of the current instance.
delete px;
In this line of code, delete is delete operator, which calls the destructor of the instance and then calls the operator in class X.
Delete to release the space occupied by the instance. The behavior of new operator and delete operator cannot and should not be changed. This is a commitment made by the C ++ standard. Operator
New corresponds to operator delete and malloc and free in C, and is only responsible for allocating and releasing space. But operator
The new allocated space must be released using operator delete, but not free, because they have different registration methods for memory usage. The opposite is true. You can reload operator new and operator delete to achieve different requirements on memory management, but you cannot reload new operator or delete
Operator to change their behavior. When operator new is reloaded, more parameters can be provided. when an object is new, additional parameters are passed through brackets after the keyword new. For example
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 passing a parameter named nothrow in this way to indicate that when the space allocation for this object fails, no exception is thrown, but 0 is returned, to be compatible with the old standard new. For example
class B {};

B* pb = new (nothrow) B();

Of course, this can only be used for those classes that use the default operator new operator. For classes that have already loaded operator new (such as X and A above), if you do not declare that you can accept the nothrow parameter, you will naturally not be able to enjoy the gift from the C ++ standard.

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.