c ++ in the new knowledge points

In practice the memory pool, find the need to reload new, but this is just overloaded overloaded operator new, so there are several new do what, let's look at records

First record a point in the class overloads operator new, default is the static member function, it is not life as a virtual function, can not call non-static member variables

 

The following text, c ++, there are three new

Said the popular point. 1, new operator (new operator) Brother, we used directly in the code it is. It does two things: After calling two kinds of new application memory and initialize the object. 2, operator new, is a function, it is also the only one to three kinds of new overloaded. It is similar to the C language malloc, for allocating memory. 3, placement new, used to initialize the object (if any, is that it calls the constructor)

1.operator new

operator new and operator delete has two overloaded versions, each supported by new expression and delete the associated expressions:

void *operator new(size_t);

void *operator new[](size_t);

void *operator delete(void*);

void *operator delete[](void*);

Into the reference type must be size_t, return type must be void *. Versions can override the following parameters for multiple

* operator new new void (size_t t1, t2 size_t); 
such that if the call is not void * p = new (10) int; t2 on the size of the size 10 and t1 has been fixed (may vary depending on the system)
this is the only thing we can overload the new, not only in the heavy-duty class, global operator new can be overloaded. If the global operator new is overloaded, then in addition to overloading in the class will override the global heavy-duty, anywhere to call new will be called to this global overloaded function in

2.new operator
when you write string * ps = new string ( "Hands up!") when, new you are using the so-called new operator, in fact, it did two things:
First, the allocation (actual size is larger than the size of the object created enough memory, because of data management ), that operator new.
Second, call the object constructor, new operator is always doing two things. The above part of the code to reflect the following about the behavior of
void * operator new new MEM = (sizeof (String));
ON * MEM Call String :: String ( "Hands up!"); // can only be done by the compiler, the user is does not allow this operation, that is to say if you want to build a heap object must use the new operator, can not be called directly as above constructor to initialize the object heap.

That operator new to allocate memory only (the same as malloc), we are able to do is overloaded operator new, to create a custom memory management scheme for their own class,
It also makes me a little and did not understand why write code that calls the constructor when overloaded operator new, but it really is called, they are actually new operator out of the ghost.

 

 

 The compiler sees the new class type or delete expressions, the first thing to see if there is a class operator new or operator delete members, if the class defines its own new and delete functions, use these functions to allocate and free memory objects otherwise, call the standard library version. If you want to customize their own unique memory allocation process, you should override the global operator new function, and then use the new operator, the new operator will call your custom operator new. Of course, you can display the calling :: operator new and :: operator delete forced to use the global library functions

 

3. Position the new expression

That placement new, locating new initialization expression has been assigned to the original memory object, he is different from other versions of new, it does not allocate memory. Instead, it accepts point to the allocated memory is good but not construction pointer and initialize an object in the memory. In fact, positioning enables us to construct a new expression in a particular subject, pre-allocated memory address. It can be any defined class constructor.

         new(place_address) type

         new(place_address) type(initializer_list)

for example,

We also can directly use it, you have got a memory p. int * p2 = new (p) int (100); so you use the placement new memory is initialized to an int, and give it an initial value of 100. In fact, here, p points to a heap memory can also be a stack. If the stack memory is so new, you do not need to delete, and can be repeated new. This is what's the use, you can dynamically allocate memory requests within a restricted area, which is a bit like the operating system to allocate memory referenced, you just use this one to hinder other than memory. He considered a security policy.

 

Guess you like

Origin www.cnblogs.com/wangshaowei/p/11583510.html