C++ : What is the order of function pointers inside vtable?In C++ why and how are virtual functions slower?Smart Pointers inside class vs Normal Pointers with Destructorfunction pointers are so complex to meAutomatic namespace resolution in C++Is declarative programming overrated?How does the base class non-virtual function get called when derived class object is assigned to base class?Function pointers vs. Derived classesWhen to mark a function as virtual?Multicast function containerAvoiding vtable pointers in objects in C++Differences between branching and virtual methods

What does it mean with the ask price is below the last price?

Remove everything except csv file Bash Script

How do I compare the result of "1d20+x, with advantage" to "1d20+y, without advantage", assuming x < y?

Can 'sudo apt-get remove [write]' destroy my Ubuntu?

Will change of address affect direct deposit?

Increase height of laser cut design file for enclosure

stdout and stderr redirection to different files

Help decide course of action for rotting windows

What are the ramifications of setting ARITHABORT ON for all connections in SQL Server?

Can I use my laptop, which says 240V, in the USA?

Is Simic Ascendancy triggered by Awakening of Vitu-Ghazi?

How does Howard Stark know this?

How did Thanos not realise this had happened at the end of Endgame?

Exception propagation: When to catch exceptions?

What are some possible reasons that a father's name is missing from a birth certificate - England?

Is there enough time to Planar Bind a creature conjured by a one hour duration spell?

Is there a need for better software for writers?

Does the 500 feet falling cap apply per fall, or per turn?

Drawing perpendicular lines, filling areas

Guns in space with bullets that return?

Why can't RGB or bicolour LEDs produce a decent yellow?

How to make the table in the figure in LaTeX?

Was the Highlands Ranch shooting the 115th mass shooting in the US in 2019

Why in a Ethernet LAN, a packet sniffer can obtain all packets sent over the LAN?



C++ : What is the order of function pointers inside vtable?


In C++ why and how are virtual functions slower?Smart Pointers inside class vs Normal Pointers with Destructorfunction pointers are so complex to meAutomatic namespace resolution in C++Is declarative programming overrated?How does the base class non-virtual function get called when derived class object is assigned to base class?Function pointers vs. Derived classesWhen to mark a function as virtual?Multicast function containerAvoiding vtable pointers in objects in C++Differences between branching and virtual methods






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















In this answer to "In C++ why and how are virtual functions slower?",
the author mentions below point:



"Get the right function address from the vtable into a register (the index where the correct function address is stored is decided at compile-time)."



As a follow up of this comment, i have some questions:



  1. What is the order of function pointers stored inside the vtable? Is it compiler dependent or all compilers have to implement it the same way?


  2. If compilers are free to implement it in their own way, then how does binary standard's like COM works. COM relies on the assumption that vtables are implemented in a uniform way by compilers, in order to pass IUnknown pointers to a component compiled by a different compiler. Isn't it?










share|improve this question




























    3















    In this answer to "In C++ why and how are virtual functions slower?",
    the author mentions below point:



    "Get the right function address from the vtable into a register (the index where the correct function address is stored is decided at compile-time)."



    As a follow up of this comment, i have some questions:



    1. What is the order of function pointers stored inside the vtable? Is it compiler dependent or all compilers have to implement it the same way?


    2. If compilers are free to implement it in their own way, then how does binary standard's like COM works. COM relies on the assumption that vtables are implemented in a uniform way by compilers, in order to pass IUnknown pointers to a component compiled by a different compiler. Isn't it?










    share|improve this question
























      3












      3








      3








      In this answer to "In C++ why and how are virtual functions slower?",
      the author mentions below point:



      "Get the right function address from the vtable into a register (the index where the correct function address is stored is decided at compile-time)."



      As a follow up of this comment, i have some questions:



      1. What is the order of function pointers stored inside the vtable? Is it compiler dependent or all compilers have to implement it the same way?


      2. If compilers are free to implement it in their own way, then how does binary standard's like COM works. COM relies on the assumption that vtables are implemented in a uniform way by compilers, in order to pass IUnknown pointers to a component compiled by a different compiler. Isn't it?










      share|improve this question














      In this answer to "In C++ why and how are virtual functions slower?",
      the author mentions below point:



      "Get the right function address from the vtable into a register (the index where the correct function address is stored is decided at compile-time)."



      As a follow up of this comment, i have some questions:



      1. What is the order of function pointers stored inside the vtable? Is it compiler dependent or all compilers have to implement it the same way?


      2. If compilers are free to implement it in their own way, then how does binary standard's like COM works. COM relies on the assumption that vtables are implemented in a uniform way by compilers, in order to pass IUnknown pointers to a component compiled by a different compiler. Isn't it?







      c++ virtual-functions com






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked May 7 at 10:26









      kartik trivikramkartik trivikram

      223




      223




















          2 Answers
          2






          active

          oldest

          votes


















          9














          It's all up to the implementation. There is no requirement that virtual functions be implemented by a per-class vtable, a per-object vtable, or any other mechanism.



          An implementation can choose to be compatible with some particular object layout and calling convention, (e.g. COM), but it isn't required to.






          share|improve this answer




















          • 10





            … and strictly speaking, it need not use a vtable at all. Though AFAIK, that's purely academic at best.

            – Deduplicator
            May 7 at 11:03






          • 3





            @Deduplicator: Well, the LLVM codebase is relatively famous for doing its own virtual-dispatch based on storing a hierarchy-specific enum field in the base class and then switching on it. It just requires a sealed hierarchy...

            – Matthieu M.
            May 7 at 14:28











          • @MatthieuM. Yes, it is. But that's user-code.

            – Deduplicator
            May 7 at 21:04











          • @Deduplicator: Sure. I see it as a proof of concept that this is feasible; it's just that the requirement of a sealed hierarchy essentially requires Whole Program Analysis, which itself requires compiling the whole program in one shot.

            – Matthieu M.
            2 days ago











          • The implementation is, however, required to define the calling convention in a fully deterministic way and stick to it, so that separately compiled objects can be linked together.

            – Jan Hudec
            2 days ago


















          7














          While it is implementation dependent, it is also fairly predictable.  Especially historically speaking, the compilers layout members in declaration order.  For fields (instance data) that means each field get assigned the next offset in the object (after alignment is rounded up as required), and, each virtual method that is introduced, is assigned next vtable slot.  (Overrides share the same vtable slot as the virtual method that they override in the base class.)



          Multiple inheritance complicates field & vtable layouts, introducing a notion of sections that group the field and vtable entries, and, the generated code has to switch between sections as their usage demands.



          Because among other things, C is the default standard for foreign function calling, many tools and programs rely on C to layout structs in the predictable order they have always done.



          C++ compilers are allowed some latitude for certain constructs these days, but as you have described, to be used with COM (which is of course a Microsoft standard, not a C++ language standard), they have also need to be predictable.



          See also:



          https://stackoverflow.com/questions/9115020/order-of-fields-in-c-c-structs



          https://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special






          share|improve this answer























          • Of course the order of fields of C structs is constrained much more by the specification. Anything with virtual members is not “standard layout object” and therefore is not bound by the rules—but in practice the compilers still follow most of them as there is no reason to create another, separate layout algorithm.

            – Jan Hudec
            2 days ago











          Your Answer








          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "131"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f391533%2fc-what-is-the-order-of-function-pointers-inside-vtable%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          9














          It's all up to the implementation. There is no requirement that virtual functions be implemented by a per-class vtable, a per-object vtable, or any other mechanism.



          An implementation can choose to be compatible with some particular object layout and calling convention, (e.g. COM), but it isn't required to.






          share|improve this answer




















          • 10





            … and strictly speaking, it need not use a vtable at all. Though AFAIK, that's purely academic at best.

            – Deduplicator
            May 7 at 11:03






          • 3





            @Deduplicator: Well, the LLVM codebase is relatively famous for doing its own virtual-dispatch based on storing a hierarchy-specific enum field in the base class and then switching on it. It just requires a sealed hierarchy...

            – Matthieu M.
            May 7 at 14:28











          • @MatthieuM. Yes, it is. But that's user-code.

            – Deduplicator
            May 7 at 21:04











          • @Deduplicator: Sure. I see it as a proof of concept that this is feasible; it's just that the requirement of a sealed hierarchy essentially requires Whole Program Analysis, which itself requires compiling the whole program in one shot.

            – Matthieu M.
            2 days ago











          • The implementation is, however, required to define the calling convention in a fully deterministic way and stick to it, so that separately compiled objects can be linked together.

            – Jan Hudec
            2 days ago















          9














          It's all up to the implementation. There is no requirement that virtual functions be implemented by a per-class vtable, a per-object vtable, or any other mechanism.



          An implementation can choose to be compatible with some particular object layout and calling convention, (e.g. COM), but it isn't required to.






          share|improve this answer




















          • 10





            … and strictly speaking, it need not use a vtable at all. Though AFAIK, that's purely academic at best.

            – Deduplicator
            May 7 at 11:03






          • 3





            @Deduplicator: Well, the LLVM codebase is relatively famous for doing its own virtual-dispatch based on storing a hierarchy-specific enum field in the base class and then switching on it. It just requires a sealed hierarchy...

            – Matthieu M.
            May 7 at 14:28











          • @MatthieuM. Yes, it is. But that's user-code.

            – Deduplicator
            May 7 at 21:04











          • @Deduplicator: Sure. I see it as a proof of concept that this is feasible; it's just that the requirement of a sealed hierarchy essentially requires Whole Program Analysis, which itself requires compiling the whole program in one shot.

            – Matthieu M.
            2 days ago











          • The implementation is, however, required to define the calling convention in a fully deterministic way and stick to it, so that separately compiled objects can be linked together.

            – Jan Hudec
            2 days ago













          9












          9








          9







          It's all up to the implementation. There is no requirement that virtual functions be implemented by a per-class vtable, a per-object vtable, or any other mechanism.



          An implementation can choose to be compatible with some particular object layout and calling convention, (e.g. COM), but it isn't required to.






          share|improve this answer















          It's all up to the implementation. There is no requirement that virtual functions be implemented by a per-class vtable, a per-object vtable, or any other mechanism.



          An implementation can choose to be compatible with some particular object layout and calling convention, (e.g. COM), but it isn't required to.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited May 7 at 18:34

























          answered May 7 at 10:37









          CalethCaleth

          6,74221420




          6,74221420







          • 10





            … and strictly speaking, it need not use a vtable at all. Though AFAIK, that's purely academic at best.

            – Deduplicator
            May 7 at 11:03






          • 3





            @Deduplicator: Well, the LLVM codebase is relatively famous for doing its own virtual-dispatch based on storing a hierarchy-specific enum field in the base class and then switching on it. It just requires a sealed hierarchy...

            – Matthieu M.
            May 7 at 14:28











          • @MatthieuM. Yes, it is. But that's user-code.

            – Deduplicator
            May 7 at 21:04











          • @Deduplicator: Sure. I see it as a proof of concept that this is feasible; it's just that the requirement of a sealed hierarchy essentially requires Whole Program Analysis, which itself requires compiling the whole program in one shot.

            – Matthieu M.
            2 days ago











          • The implementation is, however, required to define the calling convention in a fully deterministic way and stick to it, so that separately compiled objects can be linked together.

            – Jan Hudec
            2 days ago












          • 10





            … and strictly speaking, it need not use a vtable at all. Though AFAIK, that's purely academic at best.

            – Deduplicator
            May 7 at 11:03






          • 3





            @Deduplicator: Well, the LLVM codebase is relatively famous for doing its own virtual-dispatch based on storing a hierarchy-specific enum field in the base class and then switching on it. It just requires a sealed hierarchy...

            – Matthieu M.
            May 7 at 14:28











          • @MatthieuM. Yes, it is. But that's user-code.

            – Deduplicator
            May 7 at 21:04











          • @Deduplicator: Sure. I see it as a proof of concept that this is feasible; it's just that the requirement of a sealed hierarchy essentially requires Whole Program Analysis, which itself requires compiling the whole program in one shot.

            – Matthieu M.
            2 days ago











          • The implementation is, however, required to define the calling convention in a fully deterministic way and stick to it, so that separately compiled objects can be linked together.

            – Jan Hudec
            2 days ago







          10




          10





          … and strictly speaking, it need not use a vtable at all. Though AFAIK, that's purely academic at best.

          – Deduplicator
          May 7 at 11:03





          … and strictly speaking, it need not use a vtable at all. Though AFAIK, that's purely academic at best.

          – Deduplicator
          May 7 at 11:03




          3




          3





          @Deduplicator: Well, the LLVM codebase is relatively famous for doing its own virtual-dispatch based on storing a hierarchy-specific enum field in the base class and then switching on it. It just requires a sealed hierarchy...

          – Matthieu M.
          May 7 at 14:28





          @Deduplicator: Well, the LLVM codebase is relatively famous for doing its own virtual-dispatch based on storing a hierarchy-specific enum field in the base class and then switching on it. It just requires a sealed hierarchy...

          – Matthieu M.
          May 7 at 14:28













          @MatthieuM. Yes, it is. But that's user-code.

          – Deduplicator
          May 7 at 21:04





          @MatthieuM. Yes, it is. But that's user-code.

          – Deduplicator
          May 7 at 21:04













          @Deduplicator: Sure. I see it as a proof of concept that this is feasible; it's just that the requirement of a sealed hierarchy essentially requires Whole Program Analysis, which itself requires compiling the whole program in one shot.

          – Matthieu M.
          2 days ago





          @Deduplicator: Sure. I see it as a proof of concept that this is feasible; it's just that the requirement of a sealed hierarchy essentially requires Whole Program Analysis, which itself requires compiling the whole program in one shot.

          – Matthieu M.
          2 days ago













          The implementation is, however, required to define the calling convention in a fully deterministic way and stick to it, so that separately compiled objects can be linked together.

          – Jan Hudec
          2 days ago





          The implementation is, however, required to define the calling convention in a fully deterministic way and stick to it, so that separately compiled objects can be linked together.

          – Jan Hudec
          2 days ago













          7














          While it is implementation dependent, it is also fairly predictable.  Especially historically speaking, the compilers layout members in declaration order.  For fields (instance data) that means each field get assigned the next offset in the object (after alignment is rounded up as required), and, each virtual method that is introduced, is assigned next vtable slot.  (Overrides share the same vtable slot as the virtual method that they override in the base class.)



          Multiple inheritance complicates field & vtable layouts, introducing a notion of sections that group the field and vtable entries, and, the generated code has to switch between sections as their usage demands.



          Because among other things, C is the default standard for foreign function calling, many tools and programs rely on C to layout structs in the predictable order they have always done.



          C++ compilers are allowed some latitude for certain constructs these days, but as you have described, to be used with COM (which is of course a Microsoft standard, not a C++ language standard), they have also need to be predictable.



          See also:



          https://stackoverflow.com/questions/9115020/order-of-fields-in-c-c-structs



          https://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special






          share|improve this answer























          • Of course the order of fields of C structs is constrained much more by the specification. Anything with virtual members is not “standard layout object” and therefore is not bound by the rules—but in practice the compilers still follow most of them as there is no reason to create another, separate layout algorithm.

            – Jan Hudec
            2 days ago















          7














          While it is implementation dependent, it is also fairly predictable.  Especially historically speaking, the compilers layout members in declaration order.  For fields (instance data) that means each field get assigned the next offset in the object (after alignment is rounded up as required), and, each virtual method that is introduced, is assigned next vtable slot.  (Overrides share the same vtable slot as the virtual method that they override in the base class.)



          Multiple inheritance complicates field & vtable layouts, introducing a notion of sections that group the field and vtable entries, and, the generated code has to switch between sections as their usage demands.



          Because among other things, C is the default standard for foreign function calling, many tools and programs rely on C to layout structs in the predictable order they have always done.



          C++ compilers are allowed some latitude for certain constructs these days, but as you have described, to be used with COM (which is of course a Microsoft standard, not a C++ language standard), they have also need to be predictable.



          See also:



          https://stackoverflow.com/questions/9115020/order-of-fields-in-c-c-structs



          https://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special






          share|improve this answer























          • Of course the order of fields of C structs is constrained much more by the specification. Anything with virtual members is not “standard layout object” and therefore is not bound by the rules—but in practice the compilers still follow most of them as there is no reason to create another, separate layout algorithm.

            – Jan Hudec
            2 days ago













          7












          7








          7







          While it is implementation dependent, it is also fairly predictable.  Especially historically speaking, the compilers layout members in declaration order.  For fields (instance data) that means each field get assigned the next offset in the object (after alignment is rounded up as required), and, each virtual method that is introduced, is assigned next vtable slot.  (Overrides share the same vtable slot as the virtual method that they override in the base class.)



          Multiple inheritance complicates field & vtable layouts, introducing a notion of sections that group the field and vtable entries, and, the generated code has to switch between sections as their usage demands.



          Because among other things, C is the default standard for foreign function calling, many tools and programs rely on C to layout structs in the predictable order they have always done.



          C++ compilers are allowed some latitude for certain constructs these days, but as you have described, to be used with COM (which is of course a Microsoft standard, not a C++ language standard), they have also need to be predictable.



          See also:



          https://stackoverflow.com/questions/9115020/order-of-fields-in-c-c-structs



          https://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special






          share|improve this answer













          While it is implementation dependent, it is also fairly predictable.  Especially historically speaking, the compilers layout members in declaration order.  For fields (instance data) that means each field get assigned the next offset in the object (after alignment is rounded up as required), and, each virtual method that is introduced, is assigned next vtable slot.  (Overrides share the same vtable slot as the virtual method that they override in the base class.)



          Multiple inheritance complicates field & vtable layouts, introducing a notion of sections that group the field and vtable entries, and, the generated code has to switch between sections as their usage demands.



          Because among other things, C is the default standard for foreign function calling, many tools and programs rely on C to layout structs in the predictable order they have always done.



          C++ compilers are allowed some latitude for certain constructs these days, but as you have described, to be used with COM (which is of course a Microsoft standard, not a C++ language standard), they have also need to be predictable.



          See also:



          https://stackoverflow.com/questions/9115020/order-of-fields-in-c-c-structs



          https://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered May 7 at 11:26









          Erik EidtErik Eidt

          25.1k43669




          25.1k43669












          • Of course the order of fields of C structs is constrained much more by the specification. Anything with virtual members is not “standard layout object” and therefore is not bound by the rules—but in practice the compilers still follow most of them as there is no reason to create another, separate layout algorithm.

            – Jan Hudec
            2 days ago

















          • Of course the order of fields of C structs is constrained much more by the specification. Anything with virtual members is not “standard layout object” and therefore is not bound by the rules—but in practice the compilers still follow most of them as there is no reason to create another, separate layout algorithm.

            – Jan Hudec
            2 days ago
















          Of course the order of fields of C structs is constrained much more by the specification. Anything with virtual members is not “standard layout object” and therefore is not bound by the rules—but in practice the compilers still follow most of them as there is no reason to create another, separate layout algorithm.

          – Jan Hudec
          2 days ago





          Of course the order of fields of C structs is constrained much more by the specification. Anything with virtual members is not “standard layout object” and therefore is not bound by the rules—but in practice the compilers still follow most of them as there is no reason to create another, separate layout algorithm.

          – Jan Hudec
          2 days ago

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Software Engineering Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f391533%2fc-what-is-the-order-of-function-pointers-inside-vtable%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Grendel Contents Story Scholarship Depictions Notes References Navigation menu10.1093/notesj/gjn112Berserkeree

          Area configuration aggregation error after install Porto themeMagento 2.1 CE Installed but front/backend not loading/workingCSS not loading on page within Magento 2 pageCannot install module in Magento 2no commands defined in the “setup” namespace. in Magento2Magento 2: Static files are present but shows 404Why do i have to always run the commands to clean cache in Magento 2.1.8?Failure reason: 'Unable to unserialize value.'Error 500 after magento migrationIn production mode the site does not loadMagento 2 : Error 500 after installing

          Middle Expansion Olielle Resaix Definition: Uttering songs of triumph shouting with joy triumphant exulting Sejunction Journal 붙다 달 고급 품목 외출 The stretch trades the screeching tin. Definition: The act of speaking with a drawl a drawl Cough Sand Definition: An uproar a quarrel a noisy outbreak Shake Iron Publicize Horse House Baby 사과 Resaix Flaggy Jelly Temporary Unequaled Puppet A drop in the bucket Shrew 성격 회원 성질 미팅 The burn frames the tacky quality. Materialistic The smoke reduces the way. Yammoe Nondescript Cheek 얼굴 배 약하다 날리다 타다 The illegal country shows the iron. Help Rule Drearien Smoke Teaching Meaty Wasp Abraham Lincoln Jaws 진심 수리하다 Size Cork Idea Convert Think Lark John Lennon 거울 청소 군 추천하다 아이스크림