Effective C + + clause 50: Understanding the appropriate replacement time for new and delete

Source: Internet
Author: User

A

Why would anyone want to replace operator new and operator delete? Three common reasons:

(1) used to detect errors in use.

(2) in order to enhance the effect.

(3) in order to collect statistical data on use.


Two

The following is a quick development of the initial phase of global operator new, which facilitates and assists in the detection of "overruns" or "underruns".

static const int signature = 0xdeadbeef;typedef unsigned char byte;void* operator new (std::size_t size) throw (std::bad_all OC) {using namespace std;size_t realsize = size + 2 * sizeof (int); void* Pmem = malloc (realsize); if (!pmem) throw Bad_alloc ( );//writes signature to the first and last paragraph of memory * (static_cast<int*> (pmem)) = signature;* (Reinterpret_cast<int*> (static_ Cast<byte*> (PMEM) +realsize-sizeof (int)) = Signature;return static_cast<byte*> (pMem) + sizeof (int);}

The main drawback of this operator new is the neglect of the "stick to C + + rule" attitude that this particular function should have. clause 51 says that all operator new should contain a loop that calls a new_handling function repeatedly, but not here. Here we ignore it for the moment. Now just want to focus on a more subtle theme: Alignment (SNAP).

(c) The alignment of the aligned position.Many computer systems require that a particular type be placed on a specific memory address. For example, it may be required that the pointer address must be 4 times times the number (Four-byte aligned) or double the address must be 8 times times the number. Failure to adhere to this condition may result in a run-time hardware exception. Some systems are more compassionate than the other, claiming that if the alignment conditions are met, they provide better efficiency. For example, on an Intel x86 architecture, doubles can be aligned to any byte boundary, but if he is 8-byte, its access is much faster. (Personal understanding, there is no alignment, the microprocessor's reading pointer in each reading of the data must be preceded by offset, so it will affect the speed)
C + + requires all operator news returned pointers to be properly aligned (depending on the data type), and malloc is working on such requirements, and all operator new returns a malloc pointer is safe. However, we return a pointer from malloc and offset by an int size. No one is sure it's safe. If the client calls operator new in an attempt to get enough memory for a double, and we run on a machine with a "ints of 4bytes and a double must be 8bytes", we may get a pointer without proper alignment. That could cause the program to crash or slow down.

FourWhen you can reasonably replace the default new and delete on a "global" or "class-specific" basis:
(1) in order to detect the use of errors . (2) to collect usage statistics for dynamically allocated memory . (3) in order to increase the speed of distribution and restitution . (4) in order to reduce the additional space overhead caused by the default memory manager . (5) in order to compensate for the non-optimal alignment in the default allocator (suboptimal alignment). (6) in order to cluster the related objects . (7) in order to obtain non-traditional behavior .
Please remember:(1) There are many reasons to write a custom new and delete, including improving performance, debugging heap errors, and collecting heap usage information.

Effective C + + clause 50: Understanding the appropriate replacement time for new and delete

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.