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;
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:
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?If compilers are free to implement it in their own way, then how does binary standard's like
COMworks.COMrelies on the assumption thatvtablesare implemented in a uniform way by compilers, in order to passIUnknownpointers to a component compiled by a different compiler. Isn't it?
c++ virtual-functions com
add a comment |
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:
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?If compilers are free to implement it in their own way, then how does binary standard's like
COMworks.COMrelies on the assumption thatvtablesare implemented in a uniform way by compilers, in order to passIUnknownpointers to a component compiled by a different compiler. Isn't it?
c++ virtual-functions com
add a comment |
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:
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?If compilers are free to implement it in their own way, then how does binary standard's like
COMworks.COMrelies on the assumption thatvtablesare implemented in a uniform way by compilers, in order to passIUnknownpointers to a component compiled by a different compiler. Isn't it?
c++ virtual-functions com
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:
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?If compilers are free to implement it in their own way, then how does binary standard's like
COMworks.COMrelies on the assumption thatvtablesare implemented in a uniform way by compilers, in order to passIUnknownpointers to a component compiled by a different compiler. Isn't it?
c++ virtual-functions com
c++ virtual-functions com
asked May 7 at 10:26
kartik trivikramkartik trivikram
223
223
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
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
|
show 2 more comments
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
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
|
show 2 more comments
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.
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
|
show 2 more comments
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.
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.
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
|
show 2 more comments
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
|
show 2 more comments
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
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
add a comment |
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
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
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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