In-depth understanding of new and Delete in C + +

Source: Internet
Author: User

One, the details of new and delete

If you have the following code:

string New string ("a value"); string New string [ten];

The entire new statement can be divided into three steps,

First, the compiler uses a standard library function called operator new (operator new[]), which is the function of allocating a large enough primitive space. The second step, the compiler runs the corresponding constructor constructor object, the third step, the compiler will get the first address of the object returned.

When using the type *p = new type in the program (..) , the compiler looks for the corresponding operator according to the two rules:

If type is a user-defined type, it is preferred to look for the operator new function in the class and member functions of the parent class, if not, to find the user's custom version in the global function, and not to use the version of the standard library

If a user defines a function like a standard library, no redefinition is reported

The standard library defines eight users that can be overloaded in a version, where nothrow_t is defined in the header file new to represent a non-throwing exception

void*operator New(size_t);void*operator New[] (size_t);void operator Delete(void*) noexcept;void operator Delete[] (void*) noexcept;void*operator New(size_t, std::nothrow_t) noexcept;void*operator New[] (size_t, std::nothrow_t) noexcept;void operator Delete(void*, std::nothrow_t) noexcept;void operator Delete[](void*, std::nothrow_t) noexcept;

The user can customize several of the above functions (note that it is custom, not overloaded, parameter types, number of numbers to meet the requirements, even as defined by the standard library will not be error), these requirements include:

1, cannot define such new:void *operator new (size_t void *), he can only be used by the standard library

2,new must return void*, the first argument must be size_t and cannot have default parameters

3,delete must return void, the first argument must be void *

4, when we define operator delete in the class, if the second parameter is set to size_t, then the initial value of the formal parameter becomes the size of the first parameter, if the class has a virtual destructor, then the size of the size_t and operator The version of Delete will have a specific dynamic type decision, although I do not understand, for the moment first write down--!

Second, positioning new (placement new)

The above mentioned operator new () function allocates space, although we can explicitly call the object's destructor, but there is no way to call the object's constructor, we need to use a function called positioning new, which only accepts a void * parameter, and then where the pointer refers to, Call the constructor to construct an object. The specific forms are as follows:

New (PLACE_ADDR) type;

New (PLACE_ADDR) type (initilizers);

New (PLACE_ADDR) type [size];

New (PLACE_ADDR) type[size] (initilizers);

It is worth noting that the positioning of new does not specify the space of the construction object, can be the standard library operator new out, can be the user's own operator new out, can be malloc out, or even can be the stack of automatic variables, such as the array--! , whatever you want.

Iii. Summary

Although these things are not commonly used, but in the need of their own precise control of the object's spatial allocation and initialization behavior, only from the destructor and the constructor is not enough, these techniques in the STL memory allocator is widely used.

In-depth understanding of new and Delete in C + +

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.