"Yoga Mountain Night" ---- Memory Allocation (2)

zhaozj2021-02-16  76

What did new / delete do? Before following this issue, let's take a look at the following programs, there is such a block: Class A {public: a () {cout << "a is here!" << endl;} ~ a () { COUT << "a is dead!" << end1;} private: int 1; delete pa; What did new / delete do in this simple block? In fact, this program is implicitly called some things we have not seen, that is: Static void * operator new (size_t sz); static void operator delete (void * p); worth noting that these two The functions are static, so if we overload these 2 functions (we don't reload, you need to override 2 to act), should also be declared as static, if we don't declare, the system will also We automatically add. In addition, this is two memory allocation primitives, either success, or no memory is assigned. What is SIZE_T? I was also very confused when I saw this movement. After all, I haven't seen it before. Size_t is defined in is a non-symbolic integer type (not always int), used to save the size of the object, this usage is borrowed from the C language, now you should understand (I study It's been depressed for a few days. No one can ask, because I don't know if there is a 9cbs :) new a; actually do 2 things: call the OPEATOR New, allocate a sizeof (a) size memory in the free storage area Space; then call the constructor A (), on this memory space, brick tile, build our object. Also for Delete, two things are made: call destructive functions ~ a (), destroy object, call Operator Delete, release memory.

However, it is necessary to pay attention to the time when new is allocated, and there is no action for this memory space, but I just took it. This memory is still the original data (junk data), delete Time, but only release this memory, return it to the operating system, the above data is still above, so the value of the PA has not changed, but the value of the memory he pointed is not changed, but it seems to have any problems. Let's take a look at this block: int * p = new int (50000); cout << * p << "<< p << endl; delete p; cout << * p <<" << p << ENDL; we can clearly see that the data stored by the pointer P is still the original address, but the content of * P has changed, on my machine (Win2000, VC6) is always -572662307, unclear this Why, what is the system? Also think about expert advice. Here we can see that NEW works actually to ensure that mutual separation of storage allocation and initialization work can work together, but here may make beginners confused, we define a new New, But when we used, we didn't explicitly call, but let the system "mysterious" to provide this parameter. Yes, there is no doubt that the complexity is increased, but the base class provides the ability to provide allocation and release services for a collections. What are the benefits and disadvantages of new / delete? There is always a confusion from C programmers who convert from C programmers: New / delete does the Malloc / Free in the C language more advantages over? Or is it the same? In fact, even if I don't say, you should be very clear, new / delete is certainly better than Malloc / Free, or why introduced this stuff? In fact, through the above analysis, we saw that new / delete actually made a lot of Malloc / free did not do: Malloc / free just assigned and released memory; New / Delete was also responsible for completing the task of creating and destroying objects. In addition, the security of New is high, because he returned is a pointer to the object of the object, and the Void * returned for Malloc *, and it is necessary to make a mandatory type conversion, apparently this is a dangerous vulnerability. Finally, we can reload new / delete so that memory allocation is carried out according to our wishes, which is more flexible, Malloc can't. However, New / Delete is not very perfect, the biggest shortcoming is that the efficiency is low (for the default distributor), the reason is not just because of the free storage area (contrast to the stack), the specific reason is currently It is not very clear, but two possible reasons on the MCD: 1, New is just a shallow packaging of the heap dispenser (Malloc / Realloc / Free), not optimized for small memory allocation. 2, the default dispenser has versatility, which manages a memory pool, which often requires some extra space.

A variety of New General, New has many forms, but it is true that it is 2: 1, the most common form: void * operator new (std :: size_t sz) throw (std :: Bad_alloc); (ordinary) void * operator new [] (std :: size_t sz) "" VOID * OPERATOR NEW (std :: size_t sz); void * operator new [] (std :: size_t sz) This kind of person is used up, I will not give an example. 2, place new form: void * Operator new (std :: size_t count, void * ptr) throw (); (ordinary) void * operator new [] (std :: size_t count, void * ptr) throw (); (Array) To use this method, you must include the header file . The initial purpose introduced by this mechanism is to solve two related issues: 1. Place an object in a particular location; 2. Assign an object in a particular allocation area; but after introduction, it is found that this mechanism is far beyond Simple storage allocation mechanism, we can associate any logical value to a specific storage location, so that NEW has a function of universal resource manager. At the same time, the second parameter is also expanded into any type of identified type and is equipped with the corresponding nothrow version: void * operator new (std :: size_t, const st :: nothrow_t &) throw (); void * Operator new [] (std :: size_t, const st :: nothrow_t &) throw (); how can NULL? We often see that there are many beginners like to write the following code: a * p = new a (); if (p == NUL) .... Write this code may be affected by some books, Because new a () never returns NULL, if you run over the process during this process, he will throw the Bad_alloc exception, never return null, if you want him to return NULL, you should use new (nothrow) A (), not new a (). However, from an abnormal point of view, this is actually a reverse, we should try to avoid.

转载请注明原文地址:https://www.9cbs.com/read-23866.html

New Post(0)