Qsort () _ cdecl

Source: Internet
Author: User

Qsort (participant, n, sizeof (particle), & cmp); this line has not been compiled successfully. The error is:

Error c2664: "qsort": parameter 4 cannot be converted from "int (const fileinfo *, const fileinfo *)" to "int (_ cdecl *) (const void *, const void *) "you can find the answer from the Internet. You can define cmp in the following format and run it in c ++: int _ cdecl cmp (const void * p1, const void * p2). It is also possible to define cmp as the static type. I did not try it. In qsort (), parameters 4 can be written as cmp or & cmp, which is the case.

Summary:

_ Cdecl is the abbreviation of c declaration (declaration, declaration), which indicates the default function call method of c language: All parameters are pushed to the stack from right to left. These parameters are cleared by the caller, it is called manual stack clearing. The called function does not need to require the caller to pass many parameters. Too many or too few parameters are passed by the caller, and even completely different parameters do not produce compilation errors.


_ Stdcall is the abbreviation of standardcall. It is the standard call method of c ++. All parameters are pushed to the stack from right to left. If they are call class members, the last inbound pointer is the this pointer. The parameters in the stack are cleared after the called function returns. The command is retnx, and x indicates the number of bytes occupied by the parameter, the cpu automatically pops up x bytes of stack space after ret. It is called automatic stack clearing. During compilation, the function must determine the number of parameters, and the caller must strictly control the generation of parameters. There must be no more or fewer parameters. Otherwise, an error will occur after the return.

Differences between several call conventions
_ Cdecl _ fastcall and _ stdcall are call conventions (calling convention), which determine the following content: 1) the order of function parameters in the pressure stack; 2) the parameter pop-up stack is made by the caller or the caller. 3) the method for generating the function modifier name. 1. _ stdcall call Convention: The function parameters are passed from right to left through the stack. The called function clears the memory stack of the transfer parameter before returning, 2. _ cdecl is the default call Method for c and c ++ programs. Every function that calls it contains the code to clear the stack. Therefore, the size of the executable file generated is larger than that of the call to the _ stdcall function. The function uses the stack pressure mode from right to left. Note: For Variable Parameter member functions, always use the _ cdecl conversion method. 3. _ fastcall: it transmits parameters through registers (in fact, it uses ecx and edx to transmit the first two dword or smaller parameters, the remaining parameters are still transmitted from the right to the left pressure stack, and the called function clears the memory stack of the transfer parameter before returning ). 4. thiscall is only applied to "c ++" member functions. This pointer is stored in the cx register and the parameter is pressed from right to left. Thiscall is not a keyword and cannot be specified by programmers. 5. nakedcall uses 1-4 call timing. If necessary, when entering the function, the compiler will generate code to save the esi, edi, ebx, and ebp registers, when you exit the function, the code is generated to restore the content of these registers. Naked call does not generate such code. The naked call is not a type modifier, so it must be used together with _ declspec

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.