1. Replacing the operator new or operator delete provided with the standard library is typically based on the following three reasons:
1). Used to detect errors on the run. Delete "New memory" but unfortunately failure can cause memory leaks, and multiple executions of the same block of "new memory" will cause undefined behavior if operator new holds a string of dynamically assigned addresses. Operator delete removes the address from which it is easy to detect the error, and a variety of errors can result in the data "overruns" (after the write point is at the end of the allocated chunk) or "underruns" (the write point is before the start of the allocation block). If you customize a operator news and over-allocate memory, so that extra space is placed on a specific byte patterns (that is, signature, signatures), and operator delete checks that the signature is intact, To determine whether overrun or underrun occur and to remember the fact and to carry out the illegal operation of the pointer.
2). For enhanced performance. The operator new and operator deletes provided by the standard library are used primarily for general purposes-they can be accepted by long-time programs (Web server, Web server, etc.) or by programs that take less than a second to execute; They must deal with a range of requirements, including large chunks of memory, small chunks of memory, and mixed memory sizes, which must accommodate a variety of distribution patterns ranging from the dynamic allocation of small chunks of the program's survival to the continued distribution and restitution of large numbers of short-lived objects; they must also consider fragmentation. This will eventually cause the program to fail to meet the large area fast memory requirements.
Because of the variety of requirements for memory managers, the operator news and operator deletes provided by the standard library take the middle. So if you have a deep understanding of your program's dynamic memory run patterns, you'll find a custom version of operator The new and operator delete performance trumps the default version.
3). In order to collect statistics on usage. Self-defined operator new and operator Delete can help collect information such as software memory chunk size distribution, life distribution, memory return order, maximum dynamic allocation, etc.
2. The following is an example of an initial phase global operator new, which facilitates and assists in the detection of "overruns" and "underruns":
struct Const intSignature=0xDEADBEEF; typedef unsignedCharByte;//This code also has a few small errors, detailedvoid*operator New(std::size_t size)Throw(std::bad_alloc) {using namespacestd; size_t realsize=size+2+sizeof(int); void* pmem=malloc(realsize); if(!Pmem)ThrowBad_alloc (); * (static_cast<int*> (pmem)) =signatrue; * (reinterpret_cast<int*> (static_cast<byte*> (PMEM) +realsize-sizeof(int)))=signature; returnStatic_cast<byte*> (PMEM) +sizeof(int);}
"The main drawback of this operator new is that it neglects the ' stick to C + + rule ' attitude that this particular function should have." For example, clause 51 mentions that all operator news should contain a loop that calls a new-handling function repeatedly. And there's No. " In addition, there is a more subtle theme: alignment (alignment).
"Many computer architectures (computer architectures) require specific types to be placed on specific memory addresses." For example, the address of the pointer must be a multiple of 4 (four-byte aligned) or the address of the doubles must be a multiple of 8, and if this condition is not met, the runtime hardware exception may result. Some architectures such as the Intel x86 architecture doubles can be left to any other boundary, But if it is 8-byte, its access speed will be many blocks.
Therefore, the alignment (alignment) is significant in customizing operator new, because C + + requires that all operator news-returned pointers be properly aligned (depending on the data type). " malloc is working on such a request, so it is safe for malloc to return a pointer from malloc. " However, operator new does not provide such a guarantee, it returns a pointer from malloc and offsets an int size, so there is no security! If the client calls operator new to get the memory used for a double in a " INTs is 4bytes and doubles must be 8-byte to run on a machine, you may get a pointer that is not properly aligned, which may cause the program to crash or slow down. So it might not be easy to write a memory manager that works fine. many compilers have switched to debug state or log state in their memory management functions, and many platforms already have commercial products that can replace the memory manager that comes with the compiler, and the open source memory manager (such as the pool of the boost library) is also available. As a result, you may not need to customize operator new and operator delete yourself.
3. Summary--Customizing the role of operator new and operator Delete:
1). Detect Run Errors
2). In order 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 overhead of the default memory manager
5). In order to compensate for the non-optimal alignment in the default allocator (the compiler comes with the operator news does not guarantee that the dynamically allocated doubles take 8-byte alignment)
6). In order to cluster the related objects
7). In order to obtain non-traditional behavior
Effective C + + clause 50 to understand the appropriate replacement time for new and delete