answersLogoWhite

0

What is a virtual table in C?

Updated: 12/16/2022
User Avatar

Wiki User

16y ago

Best Answer

A virtual table, or "vtable", is a mechanism used in Programming languages to support dynamic polymorphism, i.e., run-time method binding. Hope ive helped. Each function has an address in memory somewhere. Function names are just pretty ways of referring to a position in memory. When a program is linked (after the compiler is finished compiling) all those names are replaced with hardcoded memory addresses.

class Foobar

{

public:

int i;

void Something() {i = 0;}

};

int main()

{

Foobar f;

f.Something();

return 0;

}

You can tell even before execution that f.Something() is referring to the Something in Foobar. The linker will be able to replace that with an address pointing directly at Something.Draw().

This works pretty well until you bring in polymorphism because the linker is unable to tell which function to call. For example in C++:

class Foobar

{

public:

int i;

Foobar() {i = -1;}

virtual void FuncA() {i = 0;}

void FuncB() {i = 10;}

};

class Foo: public Foobar

{

public:

virtual void FuncA() {i = 1;}

void FuncB() {i = 11;}

};

class Bar: public Foobar

{

public:

virtual void FuncA() {i = 2;}

void FuncB() {i = 12;}

};

void FoobarFunc(Foobar* fb)

{

cout << fb->i << endl;

fb->FuncA();

cout << fb->i << endl;

fb->FuncB();

cout << fb->i << endl << endl;

}

void FooFunc(Foo* f)

{

cout << f->i << endl;

f->FuncA();

cout << f->i << endl;

f->FuncB();

cout << f->i << endl << endl;

}

int main()

{

Foobar fb;

Foo f;

Bar b;

Foo OtherFoo;

FoobarFunc(&fb);

FoobarFunc(&f);

FoobarFunc(&b);

FooFunc(&OtherFoo);

return 0;

}

In this excerpt, both Foo and Bar inherit from Foobar. FuncA is a virtual function while FuncB is not. The integer i is being set to see the results of our actions.

Notice how in FoobarFunc, you are unable to tell beforehand what address to replace FuncA and FuncB with! In order to solve this, virtual tables are created.

Normally, functions are not stored with the data of the class at all. So in our very first Foobar example with no inheritance, the class would only be 4 bytes long (excluding any compiler magic). However, when you declare something virtual, you're actually declaring a pointer to a function so each virtual function you have will increase the class size by the size of a pointer. The pointers will be populated by the constructors under the hood which is one reason why you cannot have virtual constructors. If you have many virtual functions, you end up with many pointers, which are organized into a table... the v-table. Each class will have its own unique v-table.

When a child class is constructed, the parent class's constructor is called before the child class's constructor is called. Each classes constructor will put the addresses of all the virtual functions it has into the v-table. In our example, when Foo is instantiated, Foobar's constructor is called first. Foobar's constructor will place the memory location of Foobar.FuncA into the v-table. Once Foobar's constructor is done, Foo's constructor will execute, placing Foo.FuncA into the table.

In FoobarFunc the line fb->FuncA(), actually replaced by an address to the v-table. When that line is executed, the computer will go to that location in the v-table, fetch the address of the function, and then jump to that location in memory. Because of how the constructors setup the v-table, you end up calling the correct function.

If you look at the example, you'll notice I have included FuncB which is not declared virtual to highlight the difference between virtual and nonvirtual functions. In FoobarFunc, the parameter is Foobar. Because FuncB is not virtual, there is no entry for FuncB in the v-table. The compiler/linker will hardcode the address in the binary and cause the program to always execute Foobar.FuncB no matter what gets passed in as the parameter. No polymorphism. In FooFunc, the parameter is Foo. Foo.FuncB will always execute in that function. Again, no polymorphism.

User Avatar

Wiki User

16y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is a virtual table in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why Member functions are not virtual by default?

It depends which language you are using. Java member functions are virtual by default but C++ member functions are not. Java takes the viewpoint that if any member function is declared virtual then all member functions should be declared virtual, so they may as well be virtual by default. However, C++ takes the view that a member function should only be declared virtual if there's a specific reason to declare it virtual. Not all functions are meant to be overridden. Indeed, not all classes are meant to act as base classes. So all member functions are non-virtual by default. Purists will argue that the C++ method is the correct method. After all, there's no point in having a virtual-table if it's never going to be used. Java places the onus on the programmer to eliminate an unused virtual-table, whereas C++ simply doesn't provide one unless you explicitly declare one. However, the real reason C++ uses non-virtual methods by default is because it has to maintain compatibility with a C struct. A C struct is not a class so it has no methods (and therefore no virtual methods). It is a "plain-old-data" or POD structure. In C++, however, a struct is a class. As such, by default, it has a compiler-generated default constructor, default copy and move constructors, default copy and move assignment operators and a default destructor. It also has public access by default. However, because the compiler-generated methods are all trivial member-wise implementations, a C++ struct is backwardly compatible with a POD. Thus C code can use a C++ struct just as if it were a C struct, because both use POD structures by default. If C++ used virtual member functions by default, a struct would not be a POD by default, it would be a base class by default.


What is data view?

Is a logical description of some portion of database that is required by user to perform some task.


Is an abstract class virtual by default?

Unlike abstract class in C++, the abstract class in C# does not have any methods defined as virtual by default. The concept of virtual are not the same between C# and C++, either. Any virtual method must be defined explicitly in C#. Related to abstract methods - interestingly, an abstract class in C# does not have to have any abstract methods. However, the reverse, if a class need to have at least one abstract method, that class must be defined as abstract.


A B C ' A'B'C' by using truth table?

(A+B+C)' = A'B'C' by using truth table


Can a static function be made virtual?

No. Virtual functions are invoked according to the runtime type of the object. That is; the most-derived override is automatically invoked even when the runtime type of the object cannot be determined at compile time. This is achieved through the object's virtual table. Static methods do not have any object associated with them; they can be invoked even when no object of the type exists. Without an object, there can be no virtual table. Thus static functions cannot be virtual. They are mutually exclusive concepts.

Related questions

What is virtual function table?

A virtual function table is a table of pointers to functions.


How dynamic binding acheived in c plus plus?

Dynamic binding is achieved via virtual functions and the virtual table that is associated with every class that declares or inherits a virtual function. The virtual table (or v-table) maps every virtual function (including pure-virtual functions) to a function pointer that points to the most-derived overload. This makes it possible to invoke specific behaviour even when the runtime type of the object is unknown to the caller.


What is a sql virtual table called?

Trigger.


What is the different between a view and a create view?

View is a virtual table that do not have any data of its own but have data that is derived from another table called base table. Create view is the command used to create a view (virtual table).


Why Member functions are not virtual by default?

It depends which language you are using. Java member functions are virtual by default but C++ member functions are not. Java takes the viewpoint that if any member function is declared virtual then all member functions should be declared virtual, so they may as well be virtual by default. However, C++ takes the view that a member function should only be declared virtual if there's a specific reason to declare it virtual. Not all functions are meant to be overridden. Indeed, not all classes are meant to act as base classes. So all member functions are non-virtual by default. Purists will argue that the C++ method is the correct method. After all, there's no point in having a virtual-table if it's never going to be used. Java places the onus on the programmer to eliminate an unused virtual-table, whereas C++ simply doesn't provide one unless you explicitly declare one. However, the real reason C++ uses non-virtual methods by default is because it has to maintain compatibility with a C struct. A C struct is not a class so it has no methods (and therefore no virtual methods). It is a "plain-old-data" or POD structure. In C++, however, a struct is a class. As such, by default, it has a compiler-generated default constructor, default copy and move constructors, default copy and move assignment operators and a default destructor. It also has public access by default. However, because the compiler-generated methods are all trivial member-wise implementations, a C++ struct is backwardly compatible with a POD. Thus C code can use a C++ struct just as if it were a C struct, because both use POD structures by default. If C++ used virtual member functions by default, a struct would not be a POD by default, it would be a base class by default.


How do you view all the tables created in the database?

View is a virtual table with no data , but can be operated like any other table. It is like a virtual table through which you can view data of another table, which is known as the base table. Syntax for creating a view- CREATE VIEW as SELECT statement ;


What is data view?

Is a logical description of some portion of database that is required by user to perform some task.


What does C means on the periodic table?

C means 'carbon' in periodic table.


What is binding in c plus plus?

Binding refers to classes and their member methods. A class that has no virtual methods can simply be bound to all its methods at compile time. This is known as static binding. However classes with virtual functions require special handling as they can be overridden by derived classes. If the derived class cannot be determined at compile time, then its overridden methods are dynamically bound at runtime, using the virtual table (or v-table) to determine which version of a method must be called.


Is an abstract class virtual by default?

Unlike abstract class in C++, the abstract class in C# does not have any methods defined as virtual by default. The concept of virtual are not the same between C# and C++, either. Any virtual method must be defined explicitly in C#. Related to abstract methods - interestingly, an abstract class in C# does not have to have any abstract methods. However, the reverse, if a class need to have at least one abstract method, that class must be defined as abstract.


What is the formula to find the Virtual memory page table?

1.5 of physical memory


Does the C programming language use a virtual machine?

no