Analysis of reference and pointer usage in C + + from compilation _c language

Source: Internet
Author: User

The first is the reference case of C + + Source:

Copy Code code as follows:

void Add (int a, int b, int&c) {
c = a + B;
}


int main () {
int a = 1;
int b = 2;
int c = 0;
Add (A, B, c);

}


here is the corresponding assembly code for main:
Copy Code code as follows:

; 6:int Main () {

Push EBP
MOV EBP, esp
Sub ESP, 12; Reserve 12byte for the stack space of the calling function to store local variables a,b, c

; 7:int a = 1;

mov DWORD PTR _a$[ebp], 1 initializing a _a$ to the offset of a storage space address relative to the EBP base

; 8:int B = 2;

mov DWORD PTR _b$[ebp], 2; initialize b _b$ to b the offset of storage space address relative to EBP base

; 9:int c = 0;

mov DWORD PTR _c$[ebp], 0, _c$ C for C storage space offset from EBP base address

; 10:add (A, B, c);
Lea eax, DWORD PTR _C$[EBP]; Gets the offset of the C storage space relative to the EBP base address (that is, the offset address of the C storage unit), placed in the register EAX
push eax; The offset to save the C storage space to the stack
mov ecx, DWORD PTR _B$[EBP]; Place the value in B storage space (that is, the value of B) in the register ECX
push ecx; Save the value of the B-storage space to the stack
mov edx, DWORD PTR _A$[EBP]; Place the value in a storage space (that is, the value of a) in the register edx
push edx; Save a storage space to stack

The above push eax push ecx push edx Store the value of the original local variable a,b,c on the stack, except for C, which stores the offset address of the C storage space
So, for A,b, that is to save a copy of their worth, which is to pass the value, and C just stores the offset address of its own storage space, that is, the address
Call add@ @YAXHHAAH @z; Call the Add function, the above statement is ready to pass parameters
Add ESP, 12; Because you just pressed the stack for the call function add pass parameter, this frees up the stack space, which is the release parameter
This is why the local variables and arguments are not valid after the function call is complete because their space is freed

; 11:
; 12:}

xor eax, EAX
mov esp, EBP
Pop EBP
RET 0


The following is the assembly code for the function add:
Copy Code code as follows:

; 1:void Add (int a, int b, int&c) {

Push EBP
MOV EBP, esp

; 2:c = a + b;

mov eax, DWORD PTR _A$[EBP]; Take the value of parameter A to register EAX
add eax, DWORD PTR _B$[EBP]; the value of parameter B is added to the value of a in eax, and the result is placed in EAX
mov ecx, DWORD PTR _C$[EBP]; go to C's offset address in register ECX
mov DWORD PTR [ecx], eax; writes the result in EAX to the address cell specified by ECX, that is, the storage unit of C

; 3:}

Pop EBP
RET 0


As you can see from the above, for the transfer value, C + + does pass a copy of the value, and for the reference, although it is the form of value, but in fact, the compiler internally passed is a worthy address

The following is the case of the pointer C + + Source:

Copy Code code as follows:

void Add (int a, int b, int* c) {
*c = a + b;
}


int main () {
int a = 1;
int b = 2;
int c = 0;
Add (A, B, &c);

}


the Mian function corresponds to the assembly code:
Copy Code code as follows:

; 6:int Main () {

Push EBP
MOV EBP, esp
Sub ESP, 12;

; 7:int a = 1;

mov DWORD PTR _a$[ebp], 1

; 8:int B = 2;

mov DWORD PTR _B$[EBP], 2

; 9:int c = 0;

mov DWORD PTR _C$[EBP], 0

; 10:add (A, B, &c);

Lea eax, DWORD PTR _C$[EBP]
Push EAX
mov ecx, DWORD PTR _B$[EBP]
Push ECX
mov edx, DWORD PTR _A$[EBP]
Push edx
Call add@ @YAXHHPAH @z; Add
Add ESP, 12;
; 11:
; 12:}

xor eax, EAX
mov esp, EBP
Pop EBP
RET 0


Add function corresponding to the assembly code:
Copy Code code as follows:

; 1:void Add (int a, int b, int* c) {

Push EBP
MOV EBP, esp

; 2: *c = a + b;

mov eax, DWORD PTR _A$[EBP]
add eax, DWORD PTR _B$[EBP]
mov ecx, DWORD PTR _C$[EBP]
mov DWORD PTR [ecx], eax

; 3:}

Pop EBP
RET 0


As you can see, the pointer is the same as the assembly code of the reference, so the effect is the same

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.