new\new[]\delete\delete[] Difference

Source: Internet
Author: User

In general, objects created with new [] will be destroyed with delete[] (basic type new[] objects created by Delete can also be destroyed), for new and delete on the implementation or call malloc and free.

For basic types when destroying, the word needs to release the space of the malloc application;

However, when destroying a class type, you need to call the object's destructor in addition to the space required to release the malloc request

In the object, there may be pointer members pointing to a dynamic request for memory space, the above mentioned in the release of the request space is only free of this pointer occupied space, for the pointer to the space, you need to be released in the destructor.

New[] and delete[] for the base type and class type are slightly different in implementation:

1. For basic type new[] direct call to malloc,delete[] calls free directly, so delete and delete[] are indistinguishable.

2. For a class type, new[] In addition to calling the malloc request space, the constructor is also called to initialize each object;

More importantly, the starting address of the object array is not the address p that malloc puts back, but the four bytes after p.

So what is the four bytes that p starts to do?

Save the number of objects in the array in this four-byte place

But why do you want to save the number of objects?

The reason is the destruction of objects. When using malloc to request memory, the system is allocated according to the size of a block specification, which could result in the actual allocation of space greater than the space we requested. The actual allocated space is recorded in the system, and when this space is freed, it is only necessary to provide the starting address of the space, and the system can release the space correctly.

It is possible to release the actual request space for the base type, but for the class type, it is said that the destructor needs to be called, but how many objects need to call the destructor? Does the system not use the allocated memory/object size?

Of course not, as mentioned earlier, the system records the actual amount of space allocated, followed by some of the space we did not use. So be sure to record the number of objects.

3. When the class type calls delete[], first remove the number of objects from the first four bytes of arr from the starting position of the array n;

Then call Arr,arr+sizeof (object), ..., arr+sizeof (object) * (n-1) The destructor of the positional object;

Finally, the system releases the actual memory requested by malloc at that time through the (int *) Arr-1 address.

new\new[]\delete\delete[] Difference

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.