C ++/C function call Specification

Source: Internet
Author: User

First look at a code

void myfun(int i, int ii){    cout << i << "    " << ii << endl;}void main(){    int b = 3;    int arr[] = {6,7,8,9,10};    int *ptr = arr;    *(ptr++) += 123;    printf("%d\t%d\n", *ptr, *++ptr);    int i = 10;    myfun(i, ++i);}

Vs2008 output:

 

Function call Specification

The call specification of a function, also known as the call Convention (calling convention ). The call specification of a function determines the method by which the function is called, such as the method by which the real parameter is applied to the stack, the way the stack is rolled back, and the way the stack is released, and the method by which the function name is adapted (name mangling.

Common calling rules in windows are as follows:

1) _ cdecl: This is the default call specification for C/C ++ functions. parameters are passed from right to left and pushed to the stack. The called function is responsible for the rollback of the stack. This method applies to passing a variable number of parameters to the called function, because only the called function knows how many parameters it passes to the called function. Such as the printf function.

2) _ stdcall: the parameter is passed from right to left and pushed into the stack. The called function is used to clear the stack. When a function has a variable number parameter, it is automatically converted to the _ cdecl call specification.

3) _ thiscall: This is the default call specification for C ++ non-static member functions. variable numbers cannot be used. When a non-static member function is called, this pointer is directly stored in the ECX register and does not enter the stack. Other aspects are the same as _ stdcall.

4) _ fastcall

All interface functions must display the specified call specification unless the interface function is a non-static member function of the class.

 

Explanation of results

It is said that this question was written by Huawei. However, the author may not understand this mystery, but only know the result. Language implementation standards are undefined about the order in which function call parameters are evaluated. The result is not determined by the function call specification (from right to left), but by the specific implementation of the Microsoft Compiler: the parameter value is determined by the order from right to left. The function call pressure stack is pushed into the calculated value of the expression, rather than the expression itself.

In addition, if you call myfun (I, ++ I); instead of myfun (I, I ++ );. Enter: 11 10

 

Further confusion

   1: int main() 
   2: {
   3:     int i = 0;
   4:     printf( "%d, %d, %d, %d\n", i++, ++i, i, i++ );
   5:     return 0;
   6: }

When compiling in the Visual C ++ 2008 environment, the output result is 2, 3, 3, 0. When compiling and running in the GCC environment of mingw, the output result is 2, 2, 1, 0. It can be seen that different compilers make different policies and choices on the parameters of the printf function in stack implementation, resulting in different output results. Combined with the above call to myfun, it is found that even in the same compiler, the order of parameter values is sometimes different.

For more information, see http://fairymemory.net/2010/01/02/c/

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.