Effective c++51

Source: Internet
Author: User
Tags class operator

You need to stick to the routine when writing new and delete

This section describes what requirements should be adhered to when customizing the new and delete functions, and why these requirements are adhered to.

first, as described in the following code, when we define new, we need to take into account the size 0 memory request, why it allocates 1 bytes of memory when the memory request is 0. Let's think about it. When you define an empty ClassA, how small is sizeof (A) occupying size? Obviously occupy 1 bytes of space, this is the system to allocate memory in this case to abide by the rules, we can customize their own new, but the premise to meet the rules of the system. In fact, this is done to make the other parts of the language easy to manipulate. Considering the case of byte 0, we begin to allocate memory space, if the allocation succeeds we return what memory address, if the allocation fails we need to call the New-handler function to do after the allocation failed processing, the function is generally the use of the release of a portion of memory, Try the memory allocation again. We set up the allocation process in an infinite loop, so there must be an exit loop when you actually write the program, just as the method proposed in clause 49.

void*operator New(STD:: size_t size)Throw(STD:: Bad_alloc) {using namespace STD;if(size==0){//Processing 0-byte applicationSize=1; } while(true{Attempt to allocate size bytes;if(Assignment succeeded)returnPoints to the allocated memory;//Allocation failed to find the current new-handling functionNew_handler Globalhandler=set_new_handler (0); Set_new_handler (Globalhandler);if(Globalhandler) (*globalhandler) ();Else Throw STD:: Bad_alloc (); }}

As stated in clause 50, we define the memory allocator to be optimized for a particular class object, so we tend to encapsulate the function of the memory allocator in the base class, as in the following code:

classbase{ Public:Static void*operator New(STD:: size_t size)Throw(STD:: Bad_alloc) ...};classDerived: PublicBase{...};//assumed to redefine operator newderived* p=NewDerived;//Call Base::operator new hereIf it isclassThe exclusiveoperator New, this should be changed:void* Base::operator New(STD:: size_t size)Throw(STD:: Bad_alloc) {if(size!=sizeof(Base))return::operator New(size);//using the standard operator new......}

then, in fact, because the base class is generally smaller than the derived class, we call the standard memory allocator when the request size does not meet the derived class.

then, for the class-specific version of arrays memory allocation, you need to implement operator new[]. The only thing to do when writing operator new[] is to allocate a piece of raw memory, and the memory is large enough because you cannot do anything with an element object that has not existed in the array so far. We don't even know how many element objects this array contains. You may not know how large each object is because the base class operator new[] is likely to be called through inheritance, allocating memory to an array of derived class objects. So no more base::operator new[] assume that the size of each element object is sizeof (base), so that you cannot assume that the number of array elements is (bytes number of/sizeof (base)). In addition, the size_t parameter passed to operator new[] may have a value that is larger than the memory of the generation object, since clause 16 mentioned that dynamically allocated arrays may contain additional space to hold the number of elements.

Second , C + + guarantees that the deletion of pointers is always safe, so in the program, we operate with the following code for NULL pointers:

voidoperatordelete(voidthrow(){    if(rawMemory==0return;    归还rawMemory所指内存;}

finally, we should always remember the opposition between new and delete, and the way new is handled and the way the delete is handled is echoed. The following code:

void Base::operatordelete(voidstdthrow(){    if(rawMemory==0return;    if(size!=sizeof(Base)){        ::operatordelete(rawMemory);        return ;    }    归还rawMemory所指内存;    return ;}

Effective c++51

Related Article

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.