return value optimization is a compiler-based technique that accelerates the execution of source code by transforming the creation of source code and objects. RVO = return value optimization.
Test platform: STM32F103VG + Keil 5.15
Background:
We have a macaddress::toarray.
byte Const { return (byte*) &Value;}
Because the package needs to return the object of the byte array class ByteArray, there are
Const { return ByteArray ((byte6);}
Calling code
ByteArray bs = mac. ToArray (); Bs. CopyTo (General_reg. SHAR);
According to my shallow knowledge of C + +, when I return within ToArray, I will produce a copy of the object to the temporary object.
Then the equals sign at the caller produces a copy construct.
In fact, compile burn-write debugging, view disassembly
358: ByteArray bs = Mac. ToArray ();0x0800595c4629 MOVr1,r50x0800595e A804ADDr0,sp, #0x100x08005960 f000fe92 BL. WMacAddress:: ToArray (0x08006688)359: BS. CopyTo (General_reg. SHAR); the: 0x080059642300MOVS R3, #0x000x08005966 461AMOVr2,r30x08005968 F1040109ADDr1,r4, #0x090x0800596C A804ADDr0,sp, #0x100x0800596E f002fb8f BL. WArray:: CopyTo (0x08008090)
Directly allocates memory, which is used by incoming toarray. After the toarray, it was not seen that the second copy structure was assumed.
Here's a look at ToArray's disassembly.
0x08006688 B570PUSH{r4-r6,lr}0x0800668a4605 MOVr5,r00x0800668c0C MOVR4,R1481: Return ByteArray (byte*) &value,6);0x0800668e2206MOVS R2, #0x060x08006690 F1040108ADDR1,R4, #0x080x080066944628 MOVr0,r50x08006696 f7fffdeb BL. W _zn9bytearrayc2epkhi (0x08006270) 0x0800669a4605 MOVr5,r0482:} 0x0800669c BD70POP{R4-R6,PC}
Oh, my God! There is only one constructor in this, not a guess, to construct a local variable and then return and copy it.
Also, the memory address of this constructor is the one that is passed in externally.
This is the C + + Rvo, return value optimization technology, did not expect MDK also support.
The acquisition of this skill has allowed me to raise my C + + level from 30% to 40%
C + + return value optimization Rvo