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
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