Combat the temporary object

xiaoxiao2021-03-06  55

Temporary object, I don't know when the program will encounter this mysterious reef! Let's look at the following code:

STRING S1, S2; S1 = "ABC"; S2 = "DEF"; const char * cs = (S1 S2) .c_str (); (1) You will think of it, CS will be equal to "abcdef". However, When you run in the VC, when you put your mouse on the CS, you will find it a blank !!! This is the temporary object in the ghost! S1 S2 results will be placed in a temporary object, this temporary object When it is executed (1), the "AbcDef" pointer is sent to the CS, and after passing it to the CS, it will analyze himself, delete the pointer to the string internally stored. Direct CS points to an invalid Address! Look at (1), we will clearly find this process! 0040519D LEA EDX, [EBP-30H] // EBP-30H is the address of S2 004051A0 Push EDX004051A1 LEA EAX, [EBP-20H] // EBP-20H is the address of S1 004051A4 PUSH EAX004051A5 LEA ECX, [EBP-74H] // EBP-74H is the address of the temporary object 004051A8 PUSH ECX // ECX points to the representative temporary object String class 004051A9 Call std :: Operator (004013E4) / / Temporary object = S1 S2004051AE Add ESP, 0ch004051B1 MOV DWORD PTR [EBP-88H], EAX004051B7 MOV EDX, DWORD PTR [EBP-88H] 004051BD MOV DWORD PTR [EBP-8CH], EDX004051C3 MOV BYTE PTR [EBP-4] 2004051C7 MOV ESI, ESP004051C9 MOV ECX, DWORD PTR [EBP-8CH] // EBP-8CH is also the address of the temporary object 004051cf call dword PTR [__IMP_? C_S Tr @? $ basic_string @ du? $ char_traits @ d @ q @@ v? $ allocator @ d @ 2 @@ std @@ qbe // execution (S1 S2) .c_str () 004051d5 CMP ESI, ESP004051D7 Call _chkesp 004051DC MOV DWORD PTR [EBP-34H], EAX // EBP-34 is the address of CS execution CS = temporary object string pointer 004051DF MOV BYTE PTR [EBP-4], 1004051E3 MOV ESI, ESP004051E5 LEA ECX, [EBP -74h] 004051E8 CALL DWORD PTR [__IMP _ ?? 1? $ Basic_String @ du? $ Char_traits @ d @STD @@ v? $ Allocator @ d @ 2 @@ std @@ @@ qa @ xz // This sentence is very critical, it The destruction function of the temporary object is called, destroy yourself, execute this step, CS is also finished!

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

New Post(0)