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.
  • 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
                            • 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.

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

                              extern "C" indicates that C++ name mangling should not be used - just the plain function name, not decorated with the types of the function arguments for overload resolution.

                              M 1 Reply Last reply
                              0
                              • K kalberts

                                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 Offline
                                D Offline
                                Daniel Pfeffer
                                wrote on last edited by
                                #22

                                1. The Windows SDK was always published first in C. If any ports to other languages occurred (e.g. Borland's Borland Pascal), they were delayed by some months. 2. As my OP said, the calling convention is handled by the C compiler, so unless you are programming in Assembly language, it is irrelevant. I stand by my statement.

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

                                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.

                                  P Offline
                                  P Offline
                                  Plamen Dragiyski
                                  wrote on last edited by
                                  #23

                                  C existed during 16 bit period and I am almost sure that RET instruction did not take argument back then. Your computer most likely start in real mode, which is 16 bit, so it is still in use today. I suppose that is why they do not want to change the calling convention. P.S. fastcall prolog is usually optimized (under /O2 or more) to use arguments directly from registers when possible.

                                  1 Reply Last reply
                                  0
                                  • K kalberts

                                    extern "C" indicates that C++ name mangling should not be used - just the plain function name, not decorated with the types of the function arguments for overload resolution.

                                    M Offline
                                    M Offline
                                    Mark Kruger
                                    wrote on last edited by
                                    #24

                                    Your partly correct, indeed it indicates that C++ name mangling should not be used. But this does not mean plain function names. It's mangling the way c get's mangled. For example a function returning a 32 bit int would be something like function@4 and if u take stdcall c function by a microsoft vc compiler u often get even more mangling on the function name.

                                    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