C ++ new: What is new?

Source: Internet
Author: User

C ++ new: What is new?

9: int * obj = new int (6); 00F714CE push 4 // pressure stack 00F714D0 call operator new (0F711EAh) // call the new function to return the allocated address addr --- 0F711EAh is the address of the command jmp operator new 00F714D5 add esp, 4 // restore stack 00F714D8 mov dword ptr [ebp-0F8h], eax // assign the returned address addr to the memory space 00F714DE cmp dword ptr starting address for the ebp-0F8h [ebp-0F8h], 0 // determine whether the allocation is successful 00F714E5 je main + 51 h (0F71501h) // if it fails to jump to 0x0F71501h 00F714E7 mov eax, dword ptr [ebp-0F8h] // The starting address is the memory space of the ebp-0F8h (addr) assigned to eax00F714ED mov dword ptr [eax], 6 // set the memory space (addr) with the starting address eax to 600F714F3 mov ecx, dword ptr [ebp-0F8h] // the memory space (addr) with the starting address as the ebp-0F8h) assign the value to ecx00F714F9 mov dword ptr [ebp-100h], the value of ecx // ecx (addr) is assigned to the memory space 00F714FF jmp main + 5Bh (0F7150Bh) from the starting address to the ebp-100h) // unconditionally jump to 0x0F7150B00F71501 mov dword ptr [ebp-100h], 0 // the memory space starting from the address for the ebp-100h is set to 0 --- the allocation Failure Case 00F7150B mov edx, dword ptr [ebp-100h] // The starting address is the memory space of the ebp-100h (addr) assigned to edx00F71511 mov dword ptr [obj], edx // set the content of edx (addr) the memory space a10: delete obj; 00F71514 mov eax, dword ptr [obj] // send the memory space of obj as the starting address to eax00F71517 mov dword ptr [ebp-0ECh], eax // eax sends to memory space 00F7151D mov ecx with ebp-0ECh as the starting address, dword ptr [ebp-0ECh] // the memory space starting with ebp-0ECh is sent to ecx00F71523 push ecx // ecx press stack 00F71524 call operator delete (0F710A0h) 00F71529 add esp, 4 11: int * obj2 = new int (7); same as the allocated obj

  

* Obj is output, and "7" is returned ". Why? Obj is originally directed to the allocated address addr_obj. The delete operation is to delete the content in addr_obj and release the addr_obj memory. However, obj itself points to it. When we allocate space to obj2, the new function returns the first available address, that is, the released addr_obj. At this time, obj still points to this address, so the above result is obtained. Although this may happen occasionally, it is always possible !!! Addr's pathEax-> ptr [ebp-0F8h]-> ecx-> ptr [ebp-100h]-> edx-> ptr [obj]   What to do with the new function1. Call the object constructor to allocate space. The return address addr2. assign the returned address addr to the pointer object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.