Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Speaking of C++

Speaking of C++

Scheduled Pinned Locked Moved The Lounge
questionc++
24 Posts 15 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    What is the theory behind making the default calling convention cdecl? It leads to larger code and it's fundamentally incompatible with OS APIs. What were they thinking?

    The difficult we do right away... ...the impossible takes slightly longer.

    L OriginalGriffO C D J 8 Replies Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      What is the theory behind making the default calling convention cdecl? It leads to larger code and it's fundamentally incompatible with OS APIs. What were they thinking?

      The difficult we do right away... ...the impossible takes slightly longer.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Just googled your subject, did not read it: Calling Conventions Demystified[^] And btw: I don't care about this additional Bytes caused by cdecl vs. others. In case I use a math lib (dll) from where I use most probably only 2% of it I "waste" much more Bytes :)

      It does not solve my Problem, but it answers my question

      Richard Andrew x64R J 2 Replies Last reply
      0
      • L Lost User

        Just googled your subject, did not read it: Calling Conventions Demystified[^] And btw: I don't care about this additional Bytes caused by cdecl vs. others. In case I use a math lib (dll) from where I use most probably only 2% of it I "waste" much more Bytes :)

        It does not solve my Problem, but it answers my question

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Thank you for your opinion. :) It's good to hear from someone more knowledgeable on this subject than I.

        The difficult we do right away... ...the impossible takes slightly longer.

        L 1 Reply Last reply
        0
        • Richard Andrew x64R Richard Andrew x64

          Thank you for your opinion. :) It's good to hear from someone more knowledgeable on this subject than I.

          The difficult we do right away... ...the impossible takes slightly longer.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Quote:

          It's good to hear from someone more knowledgeable on this subject than I.

          Please use sarcasm-- :-D

          It does not solve my Problem, but it answers my question

          1 Reply Last reply
          0
          • L Lost User

            Just googled your subject, did not read it: Calling Conventions Demystified[^] And btw: I don't care about this additional Bytes caused by cdecl vs. others. In case I use a math lib (dll) from where I use most probably only 2% of it I "waste" much more Bytes :)

            It does not solve my Problem, but it answers my question

            J Offline
            J Offline
            jschell
            wrote on last edited by
            #5

            Why? Might be bias or random. But one could say that if it was thought about then perhaps because it was much more likely, when it was originally specified, that one would be calling a library that used exactly that calling convention. Probably almost always. But since it wasn't 100% they needed to account for the other cases.

            1 Reply Last reply
            0
            • Richard Andrew x64R Richard Andrew x64

              What is the theory behind making the default calling convention cdecl? It leads to larger code and it's fundamentally incompatible with OS APIs. What were they thinking?

              The difficult we do right away... ...the impossible takes slightly longer.

              OriginalGriffO Offline
              OriginalGriffO Offline
              OriginalGriff
              wrote on last edited by
              #6

              Probably, backward compatibility. There was a huge reservoir of C libraries back than, and since C++ wasn't a "new language" per se but a superset of C it would have made sense to design it to work with existing code.

              Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

              "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
              "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

              Richard Andrew x64R 1 Reply Last reply
              0
              • Richard Andrew x64R Richard Andrew x64

                What is the theory behind making the default calling convention cdecl? It leads to larger code and it's fundamentally incompatible with OS APIs. What were they thinking?

                The difficult we do right away... ...the impossible takes slightly longer.

                C Offline
                C Offline
                CPallini
                wrote on last edited by
                #7

                It is the opposite, *they* did the OS the wrong way.

                1 Reply Last reply
                0
                • OriginalGriffO OriginalGriff

                  Probably, backward compatibility. There was a huge reservoir of C libraries back than, and since C++ wasn't a "new language" per se but a superset of C it would have made sense to design it to work with existing code.

                  Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

                  Richard Andrew x64R Offline
                  Richard Andrew x64R Offline
                  Richard Andrew x64
                  wrote on last edited by
                  #8

                  That's a very interesting point, because I just assumed that C's default convention was stdapi. The reason I assumed that is that when you want to export a function from a C++ DLL, you usually enclose the declaration in extern "C". But I never considered that C's default convention was cdecl. Well played. :)

                  The difficult we do right away... ...the impossible takes slightly longer.

                  T K 2 Replies Last reply
                  0
                  • Richard Andrew x64R Richard Andrew x64

                    That's a very interesting point, because I just assumed that C's default convention was stdapi. The reason I assumed that is that when you want to export a function from a C++ DLL, you usually enclose the declaration in extern "C". But I never considered that C's default convention was cdecl. Well played. :)

                    The difficult we do right away... ...the impossible takes slightly longer.

                    T Offline
                    T Offline
                    TheGreatAndPowerfulOz
                    wrote on last edited by
                    #9

                    Richard Andrew x64 wrote:

                    But I never considered that C's default convention was cdecl.

                    Hence the name, "cdecl"

                    #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                    Richard Andrew x64R 1 Reply Last reply
                    0
                    • T TheGreatAndPowerfulOz

                      Richard Andrew x64 wrote:

                      But I never considered that C's default convention was cdecl.

                      Hence the name, "cdecl"

                      #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                      Richard Andrew x64R Offline
                      Richard Andrew x64R Offline
                      Richard Andrew x64
                      wrote on last edited by
                      #10

                      TheGreatAndPowerfulOz wrote:

                      Hence the name, "cdecl"

                      And that's why I come to this board! I always learn something interesting.

                      The difficult we do right away... ...the impossible takes slightly longer.

                      1 Reply Last reply
                      0
                      • Richard Andrew x64R Richard Andrew x64

                        What is the theory behind making the default calling convention cdecl? It leads to larger code and it's fundamentally incompatible with OS APIs. What were they thinking?

                        The difficult we do right away... ...the impossible takes slightly longer.

                        D Offline
                        D Offline
                        Daniel Pfeffer
                        wrote on last edited by
                        #11

                        The reasons are mostly historical. The cdecl convention allows for variable-length parameter lists (as required by the printf and scanf families, etc.). For all I know, it may also have been more efficient on the PDP-8 and -11, which were first used to run C. In order to maintain binary compatibility with C libraries, many C++ compilers also adopted the cdecl convention as default. Windows 1.0 was originally compiled to use the cdecl convention. Microsoft discovered that the executables would be smaller with the pascal convention (and, IIRC correctly, fit on one less diskette), so they switched.

                        Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                        T 1 Reply Last reply
                        0
                        • Richard Andrew x64R Richard Andrew x64

                          What is the theory behind making the default calling convention cdecl? It leads to larger code and it's fundamentally incompatible with OS APIs. What were they thinking?

                          The difficult we do right away... ...the impossible takes slightly longer.

                          J Offline
                          J Offline
                          Joe Woodbury
                          wrote on last edited by
                          #12

                          C++ has no default calling convention; it is implementation defined. MSDN states msvc uses cdecl by default. However, on x64, __fastcall is used unless there are variable number of arguments. Also note that while Win32 uses __stdcall by default for it's APIs, this is not mandatory for operating systems.

                          1 Reply Last reply
                          0
                          • D Daniel Pfeffer

                            The reasons are mostly historical. The cdecl convention allows for variable-length parameter lists (as required by the printf and scanf families, etc.). For all I know, it may also have been more efficient on the PDP-8 and -11, which were first used to run C. In order to maintain binary compatibility with C libraries, many C++ compilers also adopted the cdecl convention as default. Windows 1.0 was originally compiled to use the cdecl convention. Microsoft discovered that the executables would be smaller with the pascal convention (and, IIRC correctly, fit on one less diskette), so they switched.

                            Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                            T Offline
                            T Offline
                            TheGreatAndPowerfulOz
                            wrote on last edited by
                            #13

                            Daniel Pfeffer wrote:

                            so they switched.

                            bad decision

                            #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                            D 1 Reply Last reply
                            0
                            • T TheGreatAndPowerfulOz

                              Daniel Pfeffer wrote:

                              so they switched.

                              bad decision

                              #SupportHeForShe Government can give you nothing but what it takes from somebody else. A government big enough to give you everything you want is big enough to take everything you've got, including your freedom.-Ezra Taft Benson You must accept 1 of 2 basic premises: Either we are alone in the universe or we are not alone. Either way, the implications are staggering!-Wernher von Braun

                              D Offline
                              D Offline
                              Daniel Pfeffer
                              wrote on last edited by
                              #14

                              When Windows 1.0 was released, most PCs used 8088s. A high-performance machine used an 80286, and the 80386 was just coming on the market. We looked for every possible way to eke out some extra performance. Microsoft's decision made sense at the time from both the technical and the production standpoints - saving three bytes and one instruction for each function call, and shipping Windows on one less diskette. Windows was from the beginning designed to be programmed in C. Unless you were writing code in Assembly language (mostly device drivers), the difference between the calling conventions was handled by the compiler and so was transparent to the programmer. At the C level, it makes no more difference than big-endian vs. little-endian. (Now, let the Religious Wars resume... :) )

                              Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                              Richard Andrew x64R K 2 Replies Last reply
                              0
                              • Richard Andrew x64R Richard Andrew x64

                                What is the theory behind making the default calling convention cdecl? It leads to larger code and it's fundamentally incompatible with OS APIs. What were they thinking?

                                The difficult we do right away... ...the impossible takes slightly longer.

                                M Offline
                                M Offline
                                Munchies_Matt
                                wrote on last edited by
                                #15

                                I thought C++ preferred stdcall, whereas C was always cdecl. Fast call is a head fuck though, using registers for the first arguments!

                                E 1 Reply Last reply
                                0
                                • D Daniel Pfeffer

                                  When Windows 1.0 was released, most PCs used 8088s. A high-performance machine used an 80286, and the 80386 was just coming on the market. We looked for every possible way to eke out some extra performance. Microsoft's decision made sense at the time from both the technical and the production standpoints - saving three bytes and one instruction for each function call, and shipping Windows on one less diskette. Windows was from the beginning designed to be programmed in C. Unless you were writing code in Assembly language (mostly device drivers), the difference between the calling conventions was handled by the compiler and so was transparent to the programmer. At the C level, it makes no more difference than big-endian vs. little-endian. (Now, let the Religious Wars resume... :) )

                                  Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                                  Richard Andrew x64R Offline
                                  Richard Andrew x64R Offline
                                  Richard Andrew x64
                                  wrote on last edited by
                                  #16

                                  Very good information! :)

                                  The difficult we do right away... ...the impossible takes slightly longer.

                                  1 Reply Last reply
                                  0
                                  • M Munchies_Matt

                                    I thought C++ preferred stdcall, whereas C was always cdecl. Fast call is a head fuck though, using registers for the first arguments!

                                    E Offline
                                    E Offline
                                    ExcellentOrg
                                    wrote on last edited by
                                    #17

                                    Read reply of every single one and it was nice that people still care about it. Just as you can drive either on Left side or on Right side of road, in computer world, there are two primary directions to read or write data from. For higher level languages, compiler encapsulates all this complexity and provides user a neat option to switch the calling conventions. C language adopted PDP convention as its de-facto and coined the term cdecl to encapsulate the idea. The original name for opposite calling convention was PASCAL and if you go thru some old Windows API books, you will find that word. all over the place. cdecl convention allows variadic parameters very easily whereas PASCAL convention makes stack management easier. Reason why cdecl causes generates more code is usually because the underlying hardware has opposite convention and hence the compiler has to emit more opcodes to compensate for it.

                                    E 1 Reply Last reply
                                    0
                                    • Richard Andrew x64R Richard Andrew x64

                                      What is the theory behind making the default calling convention cdecl? It leads to larger code and it's fundamentally incompatible with OS APIs. What were they thinking?

                                      The difficult we do right away... ...the impossible takes slightly longer.

                                      K Offline
                                      K Offline
                                      Kirill Illenseer
                                      wrote on last edited by
                                      #18

                                      C-compatibility. Loads of issues with C++ stem from C-compatibility.

                                      1 Reply Last reply
                                      0
                                      • E ExcellentOrg

                                        Read reply of every single one and it was nice that people still care about it. Just as you can drive either on Left side or on Right side of road, in computer world, there are two primary directions to read or write data from. For higher level languages, compiler encapsulates all this complexity and provides user a neat option to switch the calling conventions. C language adopted PDP convention as its de-facto and coined the term cdecl to encapsulate the idea. The original name for opposite calling convention was PASCAL and if you go thru some old Windows API books, you will find that word. all over the place. cdecl convention allows variadic parameters very easily whereas PASCAL convention makes stack management easier. Reason why cdecl causes generates more code is usually because the underlying hardware has opposite convention and hence the compiler has to emit more opcodes to compensate for it.

                                        E Offline
                                        E Offline
                                        englebart
                                        wrote on last edited by
                                        #19

                                        Quote:

                                        Reason why cdecl causes generates more code is usually because the underlying hardware has opposite convention and hence the compiler has to emit more opcodes to compensate for it.

                                        x86 specific, RISC architectures probably have similar constraints for registers used to push arguments. cdecl causes more code because the caller is responsible for cleaning up the pushed arguments. Since cdecl allows variable argument count, only the caller knows how many arguments were actually pushed.

                                        printf("Hello", unused1, unused2);

                                        Hence the extra instruction mentioned on another post; after the call returns, the stack must be adjusted to remove the pushed arguments. std call/Pascal is a fixed argument list and the callee can adjust the argument list. On the x86 processers, "return from subroutine" has a form that allows for the stack to be adjusted to remove the pushed arguments. The std call/Pascal convention used this form to "return and adjust" as a single opcode + operand.

                                        1 Reply Last reply
                                        0
                                        • D Daniel Pfeffer

                                          When Windows 1.0 was released, most PCs used 8088s. A high-performance machine used an 80286, and the 80386 was just coming on the market. We looked for every possible way to eke out some extra performance. Microsoft's decision made sense at the time from both the technical and the production standpoints - saving three bytes and one instruction for each function call, and shipping Windows on one less diskette. Windows was from the beginning designed to be programmed in C. Unless you were writing code in Assembly language (mostly device drivers), the difference between the calling conventions was handled by the compiler and so was transparent to the programmer. At the C level, it makes no more difference than big-endian vs. little-endian. (Now, let the Religious Wars resume... :) )

                                          Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                                          K Offline
                                          K Offline
                                          kalberts
                                          wrote on last edited by
                                          #20

                                          Quote:

                                          Windows was from the beginning designed to be programmed in C.

                                          That is true if you never opened a window before it became 32 bits. The native calling convention of the original 16 bits Windows was the Pascal way of doing it.

                                          D 1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups