View the use of default parameters of c ++ functions from the compilation

Source: Internet
Author: User

In c ++, You can provide default parameters for the function. In this way, if no parameter is provided during function calling, the compiler will provide default values for the parameter. The following describes the principle of this method from the compilation.

The following is the c ++ source code:

Copy codeThe Code is as follows: int add (int a = 1, int B = 2) {// parameter a B has the default value
Return a + B;
}
Int main (){
Int c = add (); // No parameter is provided

}

The assembly code in the mian function is as follows:
Copy codeThe Code is as follows:; 4: int main (){

Push ebp
Mov ebp, esp
Push ecx; 4 bytes of storage space allocated to local variable c. ecx is a 32-bit register.

; 5: int c = add ();

Push 2; press stack 2, that is, the default value of parameter B in the add function. Here, the parameter pressure stack direction is from right to left.
Push 1; press stack 1, which is the default value of the parameter in the add function
Call? Add @ YAHHH @ Z; call the add function.
Add esp, 8; release the storage space when parameters are provided for add.
Mov dword ptr _ c $ [ebp], eax; The eax registers store the add function return value, and write it into variable c.

; 6:
; 7 :}

Xor eax, eax
Mov esp, ebp
Pop ebp
Ret 0

The assembly code of the add function is as follows:
Copy codeThe Code is as follows :? Add @ YAHHH @ z proc; add

; 1: int add (int a = 1, int B = 2 ){

Push ebp
Mov ebp, esp

; 2: return a + B;

Mov eax, dword ptr _ a $ [ebp]; write the value of parameter a to the Register eax
Add eax, dword ptr _ B $ [ebp]; remove the value of parameter B and add it to the value in eax, and save the result to the eax register.

; 3 :}

Pop ebp
Ret 0
? Add @ YAHHH @ Z ENDP

The following shows only one parameter value.

First look at the c ++ source code:

Copy codeThe Code is as follows: int add (int a = 1, int B = 2) {// parameter a B has the default value
Return a + B;
}
Int main (){
Int a = 3;
Int c = add (a); // only provides parameters for

}

The assembly code in the main function is as follows:
Copy codeThe Code is as follows:; 4: int main (){

Push ebp
Mov ebp, esp
Sub esp, 8; the esp register moves 8 bytes as the stack pointer and reserves storage space for local variables a and c.

; 5: int a = 3;

Mov dword ptr _ a $ [ebp], 3; write 3 to the bucket where local variable a is located

; 6: int c = add (a); // No parameter is provided.

Push 2; press 2 to provide the default value of the B Parameter
Mov eax, dword ptr _ a $ [ebp]; extract the value of a and put it in the register eax.
Push eax; press the values in eax to the stack and provide the value of parameter a. Here, the default value 1 is not provided.
Call? Add @ YAHHH @ Z; call the add function.
Add esp, 8; release the 8byte space allocated to the parameter just now for calling function add
Mov dword ptr _ c $ [ebp], eax; eax stores the result of calling the function and writes it to the bucket where c is located.

; 7:
; 8 :}

Xor eax, eax
Mov esp, ebp
Pop ebp
Ret 0

We can see that, unlike the above, the default parameter value is not provided for.

The following is the assembly code of the add function, which is the same as the first case and remains unchanged:

Copy codeThe Code is as follows :? Add @ YAHHH @ z proc; add

; 1: int add (int a = 1, int B = 2) {// parameter a B has the default value

Push ebp
Mov ebp, esp

; 2: return a + B;

Mov eax, dword ptr _ a $ [ebp]
Add eax, dword ptr _ B $ [ebp]

; 3 :}

Pop ebp
Ret 0
? Add @ YAHHH @ Z ENDP

Here, because a definite value is provided for parameter a, the compiler only provides the default value for parameter B. It is conceivable that if explicit parameter values are provided for the add function, the compiler will not provide default values for parameters a and B.

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.