0

I know the how virtual function and dynamic polymorphism works. I konw that compiler will add a v-ptr in the base class which will be inherited by subsequent derived classes. Also that, compiler will create v-tables for each class and will keep the function pointers of most derived version of the virtual functions with respect to each of the classes.

Now, my question here is where does this v-table gets stored? Is it in the code segment? From where memory is allocated for these table(s). Please share the internal details.

I also know that the implementation may vary from one compiler to the other. But one can mention one particular compiler and the implementation specific to that compiler in his/her answer.

paper.plane
  • 1,201
  • 10
  • 17
  • 5
    How can we share the "internal details" when you didn't specify a compiler? This is all an *implementation detail*. A popular one I'll grant you, but still compiler dependent and not mandated by the language alone. – StoryTeller - Unslander Monica Jan 29 '18 at 10:12
  • 1
    ... is this "too broad"? – user202729 Jan 29 '18 at 10:13
  • The john compiler (the one that exists in my head) stores the vtable in the data segment (it is data after all) and allocates the memory statically (it is fixed at compile time after all). – john Jan 29 '18 at 10:15
  • @StoryTeller I have already mentioned in my question that, one can mention any compiler name and corresponding implementation. – paper.plane Jan 29 '18 at 10:16
  • Making your question too broad, as user202729 pointed out. – StoryTeller - Unslander Monica Jan 29 '18 at 10:16
  • @StoryTeller, will it be good if I ask for GCC? I did not do that because someone knowing about some other compiler may skip to answer in that case. – paper.plane Jan 29 '18 at 10:18
  • If you want a discussion of compiler implementation techniques, [so] is not the proper place. It's too broad for the format, nothing more to it. – StoryTeller - Unslander Monica Jan 29 '18 at 10:19
  • Possible duplicate of [Where in memory is vtable stored?](https://stackoverflow.com/questions/1905237/where-in-memory-is-vtable-stored) – user7860670 Jan 29 '18 at 10:23
  • @VTT Right, it is sort of duplicate, but none has has answered about vtable in that question. Everyone talked about vptr – paper.plane Jan 29 '18 at 10:26
  • 1
    Why not just build a toy project in the compiler(s) you care about and have access to, and _look_? You can inspect the symbol tables and/or decompile the new-expression code to find out. – Useless Jan 29 '18 at 10:29
  • ^^^ what @Useless says is not useless:) Design and build some simple project, look at the assembly. You can run it and step through with the debugger so you can see how the vptr's, vtables, and method calls work with inheritance, abstract and override methods etc. Much more instructive than guesswork:) – Martin James Jan 29 '18 at 10:33
  • As a rule, it's stored as some kind of **read only** data. – curiousguy Feb 05 '18 at 01:17

1 Answers1

0

Indeed, it depends on compiler and platform. GCC on Linux puts them in .rodata segment of the ELF file. Visual C++ puts them in .rdata segment of EXE/DLL file.

In both cases, the memory is not allocated anywhere, it’s memory mapped from the execution file, just like the code.

Soonts
  • 20,079
  • 9
  • 57
  • 130