The new in C ++ is actually a very confusing term. It has two different meanings: the new operator (New
Operator) and new functions (operator new) are worth record.
A new operator
The most common operator is the new operator, for example:
String
* STR = new string ("Test
New ");
As an operator, like sizeof, new is built-in C ++. You cannot make any changes to it except using it.
New allocates a block of memory on the stack and automatically calls the class constructor.
2. New Function
The second type is the new function. In fact, the memory allocated by the new operator is the new function. The prototype is:
Void * operator
New (size_t size );
The new function returns a void pointer, an uninitialized memory. As you can see, this is similar to the malloc action in C. You can reload the new function and add additional parameters, but you must ensure that the first parameter must be of the size_t type, it specifies the size of the allocated memory block. c ++ allows you to do so. Of course, this is not necessary in general. If the new function is reloaded, the new function is called when the new operator is used.
If you use the new function and the statement string
* STR = new string ("test new") Relative code is like the following:
String * STR =
(String *) operator new (sizeof (string ));
Str. String ("Test
New "); // Of course, this call is invalid, but the compiler does not have this limit.
This is not complete yet, and there is a third new.
Three placement new
Third, placement new, which is also a usage of New as a function. It allows you to allocate an object on an existing memory, the data in the memory will not be overwritten or overwritten by you. Placement new is also called by the new operator. The call format is:
New (buffer) type (size_t
Size );
Let's take a look at the following code:
Char STR [22];
Int DATA = 123;
Int * pA = new (& Data) int;
Int * pb = new (STR) int (9 );
The result is * pA = 123 (the original data is not overwritten), and * pb = 9 (the original data is overwritten). We can see that placement new is not allocated with new memory, you can also use the memory allocated on the stack, not limited to the heap.
To use placement new, you must include <New> or <New. h>
In fact, the placement new is the same as the second one, but the parameter is added, which is a heavy load of the function new. The syntax format is:
Void * operator
New (size_t, void * buffer );
It may look like this:
Void * operator
New (size_t, void * buffer) {return buffer ;}
Delete corresponds to new, and memory needs to be recycled. Otherwise, it will be leaked. Write it again next time and recall the contents of today.
Summary
1. function new
Void * operator
New (size_t size); allocate a block of memory on the stack, and placement new (void * operator new (size_t, void * buffer); create an object on an existing memory, if you already have a piece of memory, placement new will be very useful. In fact, it is widely used in STL.
2. Operator new
There is nothing to say about the most commonly used new.
3. The new function does not automatically call the class constructor because it does not know the allocated memory type. The new operator automatically calls the class constructor.
4. The new function allows overloading, while the new operator cannot.
5. The corresponding Delete is followed.