Thread: _Cdecl

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    19

    _Cdecl

    i have this somewhere
    " In __cdecl functions, arguments are pushed right to left and the caller is responsible for cleaning up the stack "
    What actually is it.
    Can we use _cdecl in out function prototyping...

  2. #2
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    Quote Originally Posted by sunil21
    i have this somewhere
    " In __cdecl functions, arguments are pushed right to left and the caller is responsible for cleaning up the stack "
    What actually is it.
    Code:
    void foo(int a1, int a2, int a3);
    
    if you disassemble your code you will see, that _cdcel and _stdcall look like that.
    
    _cdcel call:
    __asm
    {
      push a1
      push a2
      push a3
      call foo
    }
    
    _stdcall call:
    __asm
    {
      push a3
      push a2
      push a1
      call foo
    }
    }
    You see, left to right and right to left. And foo() will "pop" arguments (cleans the stack) before returning.

  3. #3
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    What is the relative advantage in each case? Also, what is meant by the line "and the caller is responsible for cleaning up the stack" in case of _cdecl? Does that mean in _stdcall's case cleaning of stack is not required (then, will the compiler insert the code to do it?).?

    -Harsha
    Help everyone you can

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    84
    There's almost no advantage in any case. That's why they are called conventions and not some methods or something. The only difference is that if you calling lot's of external functions (like Win32 API) your code will be few bytes shorter (3 bytes per function call).
    If you have pushed something in stack you have to clean it up, otherwise you'd have stack overflow pretty soon. Usually it's done by "add esp, <number of bytes>", but line can be in caller or callee. In case of _cdcel it's caller (with exeption of functions that take "..." as argument), and in case of _stdcall it's callee.
    Edit: you don't have to insert any code if you are not using assembly. you just have to specify wich convention function uses and compiler will do the rest.
    Last edited by iwabee; 08-21-2004 at 08:16 AM.

  5. #5
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    There are certain functions that have a varaible argument list such as printf(). With the cdecl it allows you create functions that take a variable arguement list. however

    " The __cdecl calling convention creates larger executables than __stdcall, because it requires each function call to include stack cleanup code" --MSDN library
    http://msdn.microsoft.com/library/de...re___cdecl.asp
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  6. #6
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Ok, Thank you... Learnt a new thing now.....
    Help everyone you can

Popular pages Recent additions subscribe to a feed