Overhead of C++ inheritance with no virtual functions

11,003

Solution 1

There might a be slight memory overhead (due to padding) when using inheritance compared to copy and past, consider the following class definitions:

struct A
{
  int i;
  char c1;
};

struct B1 : A
{
  char c2;
};


struct B2
{
  int i;
  char c1;
  char c2;
};

sizeof(B1) will probably be 12, whereas sizeof(B2) might just be 8. This is because the base class A gets padded separately to 8 bytes and then B1 gets padded again to 12 bytes.

Solution 2

It will take very slightly longer to compile, and there will be no additional runtime overhead. From the optimizer's perspective, non-virtual methods are the same as procedures -- they can be called using only their memory address, without overhead from a virtual method table.

Solution 3

If you forget virtual inheritance, having a base class is equivalent, memory and performance wise, to having a member of the same class. Excepted that it can sometimes be even better (for instance an empty class has a size of at least one but having an empty base class may often have no overhead).

Solution 4

If you might have a pointer of type Base* that points to an object of type Derived*, you probably need a virtual destructor and your original premise no longer applies. If the derived class has an empty destructor, and it has no members or they're all POD types, you can get away without a virtual destructor, but it's usually better to play it safe and make it virtual from the start.

The compiler will generate a direct call to the code implementing each non-virtual member function, thus there is no overhead.

Solution 5

Not really, it only increased the memory by the base class. You can read more in here in C++ FAQ

Share:
11,003

Related videos on Youtube

jameszhao00
Author by

jameszhao00

rabble

Updated on August 15, 2020

Comments

  • jameszhao00
    jameszhao00 almost 4 years

    In C++, what's the overhead (memory/cpu) associated with inheriting a base class that has no virtual functions? Is it as good as a straight up copy+paste of class members?

    class a
    {
    public:
        void get();
    protected:
        int _px;
    }
    
    class b : public a
    {
    
    }
    

    compared with

    class a
    {
    public:
        void get();
    protected:
        int _px;
    }
    
    class b
    {
    public:
        void get();
    protected:
        int _px;
    
    }
    
    • Admin
      Admin almost 15 years
      What's the point in using (public) inheritance if you have no virtual functions? You will need at least a virtual destructor.
    • vehomzzz
      vehomzzz almost 15 years
      @Neil could be for code re-use, avoiding reinvention of the wheel
    • Admin
      Admin almost 15 years
      In that case he should use composition, private inheritance or free functions.
    • jameszhao00
      jameszhao00 almost 15 years
      Noted. :) I'll wait a bit next time.
    • sbi
      sbi almost 15 years
      @Neil: I believe streams do not have virtual functions, yet they derive from each other. What's wrong with this?
    • Admin
      Admin almost 15 years
      @sbi They all derive from ios_base, which has a virtual destructor.
    • sbi
      sbi almost 15 years
      @Neil: Sure, the dtor is virtual, but you aren't arguing that this makes the hierarchy polymorphic, are you? If we wouldn't want to allow dynamic allocation of streams, there was no need for any virtual and the derivation would still make sense.
    • sbi
      sbi almost 15 years
      I have read this, and I don't believe it. When doing C++, I mostly don't do real OO nowadays, although I combine derivation and virtual functions with compile-time polymorphy. I'm more the multi-paradigm guy. Anyway, I remember GCC warned about missing virtual dtors for classes that were never new'ed -- let alone deleted through a base class pointer. There was this guy porting the code to Linux who would mindlessly put in virtual dtors every time GCC said so, sometime adding a vtable to zero-sized classes that were pure compile-time constructs. It drove me nuts.
    • Qwertie
      Qwertie over 14 years
      Not having a virtual destructor makes sense if it's a "plain old data" structure that needs to do little or no work in the destructor. Also if you plan to instantiate a million copies, dealing with the problems of going without a virtual destructor may be worth it.
  • jameszhao00
    jameszhao00 almost 15 years
    I'm aware there's a function-table cost as soon as virtual functions are introduced. I'm wondering how the C++ compiler processes inheritance with no polymorphism.
  • vehomzzz
    vehomzzz almost 15 years
    Why would it take longer to compile? just curious. I thought compiler's were smart enough these days :)
  • John Millikin
    John Millikin almost 15 years
    The increased time will be only a few milliseconds, since the compiler will have to construct the inheritance tree. It's not worth caring about.
  • vehomzzz
    vehomzzz almost 15 years
    for one, it doesn't allocate memory for vtable, doesn't create a vpointer in the base class, thus both compiler and runtime takes less than with virtual function/dtors. what are you trying to accomplish?
  • jameszhao00
    jameszhao00 almost 15 years
    All the other answers are valid, but this is something I didn't know/think of before.
  • einpoklum
    einpoklum about 8 years
    Don't you mean "the same as non-inherited methods"?