__cdecl and __stdcall

Q. Difference between __cdecl and __stdcall ?

A. Responsibility of cleaning up of arguments on stack is caller’s in case of __cdecl and is callee’s in case of __stdcall. Default call is __cdecl.

some more points to keep in mind:

> Since __stdcall does stack cleanup, the (very tiny) code to perform this task is found in only one place, rather than being duplicated in every caller as it is in __cdecl. This makes the code very slightly smaller, though the size impact is only visible in large programs.

> Variadic functions like printf() are almost impossible to get right with __stdcall, because only the caller really knows how many arguments were passed in order to clean them up. The callee can make some good guesses (say, by looking at a format string), but the stack cleanup would have to be determined by the actual logic of the function, not the calling-convention mechanism itself. Hence only __cdecl supports variadic functions so that the caller can do the cleanup.

> There isn’t really a “right or wrong” with respect to which one is best, but it is positively fatal to “mix and match”. The general principle is “the stack-cleanup must match the arg-pushing”, and this only happens when caller and callee know what the other is doing. Calling a function with the “wrong” convention will destroy the stack.

Details are available here .

Advertisements