vtable

Performance hit of vtable lookup in C++

与世无争的帅哥 提交于 2019-11-29 23:05:44
I'm evaluating to rewrite a piece of real-time software from C/assembly language to C++/assembly language (for reasons not relevant to the question parts of the code are absolutely necessary to do in assembly). An interrupt comes with a 3 kHz frequency, and for each interrupt around 200 different things are to be done in a sequence. The processor runs with 300 MHz, giving us 100,000 cycles to do the job. This has been solved in C with an array of function pointers: // Each function does a different thing, all take one parameter being a pointer // to a struct, each struct also being different.

Virtual Table C++

为君一笑 提交于 2019-11-29 22:25:06
I read a lot of people writing "a virtual table exists for a class that has a virtual function declared in it". My question is, does a vtable exists only for a class that has a virtual function or does it also exist for classes derived from that class. e.g class Base{ public: virtual void print(){cout<<"Base Print\n";} }; class Derived:public Base{ public: void print(){cout<<"Derived print\n";} }; //From main.cpp Base* b = new Derived; b->print(); Question: Had there been no vtable for class derived then the output would not have been "derived print". So IMO there exists a vtable for any class

Loading an EXE as a DLL, local vftable

你说的曾经没有我的故事 提交于 2019-11-29 20:22:43
问题 I have an exe named test.exe which is usually used as a stand-alone application. I want to use this exe as a module (a dll) inside another application, app.exe. The code in test.exe does something really simple like: void doTest() { MyClass *inst = new MyClass(); inst->someMethod(); } Where someMethod() is virtual and MyClass has a virtual d'tor. doTest() is exported from test.exe and thus a lib called test.lib is created app.exe is linked with this lib to statically load test.exe when it

How is the deletion of a pointer detected using dynamic cast

限于喜欢 提交于 2019-11-29 14:08:20
问题 As shown here, one can use dynamic_cast to detect a deleted pointer: #include <iostream> using namespace std; class A { public: A() {} virtual ~A() {} }; class B : public A { public: B() {} }; int main() { B* pB = new B; cout << "dynamic_cast<B*>( pB) "; cout << ( dynamic_cast<B*>(pB) ? "worked" : "failed") << endl; cout << "dynamic_cast<B*>( (A*)pB) "; cout << ( dynamic_cast<B*>( (A*)pB) ? "worked" : "failed") << endl; delete pB; cout << "dynamic_cast<B*>( pB) "; cout << ( dynamic_cast<B*>

Size of virtual pointer-C++

我的未来我决定 提交于 2019-11-29 11:03:32
What is the size of virtual pointer(VPTR) for a virtual table in C++? Also this is not a homework question...just a question that came to my mind while I was reading a C++ book. An excellent article related to this topic is Member Function Pointers and the Fastest Possible C++ Delegates . This article delves deeply into the implementation of member function pointers for many different compilers. This article talks about all the nuances of vtable pointers particularly in light of multiple (and virtual) inheritance. Note that in order to gracefully handle multiple inheritance, there can be more

How many vptr will a object of class(uses single/multiple inheritance) have?

China☆狼群 提交于 2019-11-29 10:35:42
问题 How many vptrs are usually needed for a object whose clas( child ) has single inheritance with a base class which multiple inherits base1 and base2. What is the strategy for identifying how many vptrs a object has provided it has couple of single inheritance and multiple inheritance. Though standard doesn't specify about vptrs but I just want to know how an implementation does virtual function implementation. 回答1: Why do you care? The simple answer is enough , but I guess you want something

C++: Class specialization a valid transformation for a conforming compiler?

二次信任 提交于 2019-11-29 06:11:49
问题 Hopefully this isn't too specialized of a question for StackOverflow: if it is and could be migrated elsewhere let me know... Many moons ago, I wrote a undergraduate thesis proposing various devirtualization techniques for C++ and related languages, generally based on the idea of precompiled specialization of code paths (somewhat like templates) but with checks to choose the correct specializations are chosen at runtime in cases where they cannot be selected at compile-time (as templates must

How are vtables implemented in c++ and c#?

不问归期 提交于 2019-11-29 05:16:21
问题 Lets have this situation (in c++, in c# classes A,B are interfaces): class A { virtual void func() = 0; }; class B { virtual void func() = 0; }; class X: public A, public B { virtual void func(){ var = 1; } int var;}; X * x = new X; // from what I know, x have 2 vtables, is this the same in c#? A * a = (A*)x; // a == x B * b = (B*)x; // here b != x, so when calling b->func(), how is the address of var correct? Does the c# compiler create always one vtable? Does it make any pointer fixups when

Why vptr is not static?

こ雲淡風輕ζ 提交于 2019-11-29 03:18:08
问题 Every class which contains one or more virtual function has a Vtable associated with it. A void pointer called vptr points to that vtable. Every object of that class contains that vptr which points to the same Vtable. Then why isn't vptr static ? Instead of associating the vptr with the object, why not associate it with the class ? 回答1: The runtime class of the object is a property of the object itself. In effect, vptr represents the runtime class, and therefore can't be static . What it

How to use delay loading with a DLL that exports C++ classes

别说谁变了你拦得住时间么 提交于 2019-11-29 01:52:37
I have a DLL one.dll that uses a class TwoClass exported from two.dll via class __declspec(dllexport) . I'd like one.dll to use /delayload for two.dll , but I get a link error: LINK : fatal error LNK1194: cannot delay-load 'two.dll' due to import of data symbol '"__declspec(dllimport) const TwoClass::`vftable'" (__imp_??_7TwoClass@@6B@)'; link without /DELAYLOAD:two.dll That's in a Release build; in a Debug build it works. (I don't know what the difference is between Release and Debug in terms of vtable exports, nor can I find any compiler switches or pragmas to control it.) How can I use