The difference between "__stdcall", "C + +", __cdcel and __fastcall

Source: Internet
Author: User

__stdcall, __cdecl, and __fastcall are three function invocation protocols, which affect the way the function parameters are stacked, how the data is purged in the stack, the modifiers of the compiler function names, and so on. As shown, you can set the default calling protocol for all functions in the IDE environment, and you can set the invocation protocol of this function separately when the function is defined.

  1. Calling protocol common occasions
    1. __stdcall:windows API default function call protocol.
    2. __cdecl:c/c++ the default function call protocol.
    3. __fastcall: Suitable for applications where performance requirements are high.
  2. function parameter in stack mode
    1. __stdcall: Function arguments are left-right into the stack.
    2. __cdecl: Function arguments are left-right into the stack.
    3. __fastcall: Parameters that are not greater than 4 bytes from the left are placed into the CPU's ECX and edx registers, and the remaining parameters are stacked from right to left.
    4. Problem one: __fastcall in the register to put not more than 4 bytes of parameters, it is high performance, suitable for applications that require high performance.
  3. In-Stack data removal method
    1. __stdcall: After the function call is finished, the data inside the stack is purged by the called function.
    2. __CDECL: After the function call is finished, the data inside the stack is cleared by the function caller.
    3. __fastcall: After the function call is finished, the data inside the stack is purged by the called function.
    4. Problem one: Different compiler sets the stack structure is not the same, across the development platform by the function caller to clear the stack of data is not OK.
    5. Problem two: The parameters of some functions are mutable, such as the printf function, which can only be cleared by the function caller of the data in the stack.
    6. Problem three: When the caller clears the data in the stack, each call contains code that clears the data in the stack, so the executable file is larger.
  4. C language Compiler function name decoration rules
    1. __stdcall: After compiling, the function name is modified to "[email protected]".
    2. __CDECL: After compilation, the function name is decorated as "_functionname".
    3. __fastcall: After compiling, the function name is decorated with "@[email protected]".
    4. Note: "functionname" is the function name and "number" is the parameter byte count.
    5. Note: Function calls cannot be implemented if a different function call protocol is used when the function is implemented and the function is defined.
  5. C + + language compiler function name decoration rules
      1. __stdcall: After compiling, the function name is decorated as "? [Email protected] @YG ******@z ".
      2. __CDECL: After compiling, the function name is decorated as "? [Email protected] @YA ******@z ".
      3. __fastcall: After compiling, the function name is decorated as "? [Email protected] @YI ******@z ".
      4. Note: "******" returns the value type and the parameter type table for the function.
      5. Note: Function calls cannot be implemented if a different function call protocol is used when the function is implemented and the function is defined.
      6. The C and C + + languages are not able to invoke each other's functions without special processing.

Original address: http://blog.sina.com.cn/s/blog_701526f40100lcy6.html

The difference between "__stdcall", "C + +", __cdcel and __fastcall

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.