20666

Does virtual inheritance increase the size of derived class? [duplicate]

Question:

<blockquote>

<strong>Possible Duplicate:</strong><br /><a href="https://stackoverflow.com/questions/2038717/object-size-with-virtual" rel="nofollow">object size with virtual</a>

</blockquote>

Does virtual inheritance change the size of the derived class? I executed the following code, where I have two derived classes one virtually inherited and the other non virtually inherited:

class A { public: int a; virtual void a1(); }; class non_vir_der: public A{ public: int c; virtual void aa(); }; class vir_der: public virtual A{ public: int d; virtual void bb(); }; int main() { cout<<sizeof(non_vir_der)<<"\n"; cout<<sizeof(vir_der)<<"\n"; return 0; } <hr />

output:

12 (imo: 4(int a)+ 4(int c)+ 4(vir ptr))

<h2>16 (extra 4?)</h2>

to check again if i had missed something, i tried the minimum code required, by deleting all ints in the classes and the output was:

4

<h2>4</h2>

the second output indicates that the two derived classes are of same size. Why is the size of vir_der 16 in the first run, why is it not 12?

Answer1:

This behavior is completely implementation-specific and there are no guarantees about what will happen. That said, most compilers implement virtual inheritance by having a single copy of the base class lying around, then having each instance of the derived class store a pointer inside its class body that points to that one unique instance. That way, if you cast the object to its virtual base, the compiler can emit code to find out where that object is. On a 32-bit system, this means that virtual inheritance might increase the size of the object by four bytes, as you've observed.

So why is there no difference if there are no data members? I'm not sure, but I'm guessing that this is because the compiler is smart enough to realize that if you have a virtual base class with no data members, you don't ever actually need to access anything contained within it. Consequently, the object can be optimized by eliminating the extra pointer to the virtual base, since there's never a way that it could be accessed. I could be wrong about this, though.

Hope this helps!

Answer2:

Basically,when you declare a function or class virtual,the compiler instantiates a virtual

pointer which is required in case of UPCASTING .Actually,a virtual pointer table is created

where all virtual instances are stored with their actual address .Thus,when you inherited

virtual class in 2nd case ,an extra *vptr got created.

Now the second case, why is that 4 in both cases...That's completely compiler specific.

It should have been 1 in 1st case and 4 in 2nd case according to simple logic.

Thanx

Recommend

  • Check if there is enough memory before allocating byte array
  • RXJava how to try to get next after x time
  • Why video is not playing, first time it will play on dynamically changing it will not play
  • Meteor load scripts, CSS specific to pages
  • How to use nix's ioctl?
  • Why can't i see some C++ DLL ( support CLR ) in C# project ?
  • ASP.NET application developed in 32 bit environment not working in 64 bit environment
  • Change message body before sending in outlook 365
  • Grid on top of Google maps produces gaps in squares
  • Copy and reformat cells from one worksheet to another
  • Errors with Codename One “Send iOS Build” and “Send Android Build”
  • Using gson, how to exclude a numeric type (double, integer) with default value from serialization?
  • implementing euclidean distance based formula using numpy
  • touch events not working in android phonegap webview (or even built-in browser)
  • Docker: unix “who” command doesn't work inside container
  • How to change WebBrowser fullscreen video mode?
  • Multiple JSliders reacting to each other to always equal 100 percent
  • Python read xml with related child elements
  • MySQL - Filter records which date is biggest
  • Storing the Cursor for App Engine Pagination
  • VB.NET and LINQ - Group by in a DataTable
  • How to sort by Lucene.Net field and ignore common stop words such as 'a' and 'the
  • Why am I getting an Argument exception when creating event handler dynamically?
  • Protractor Page objects - TypeError: Object # has no method 'methodName'
  • Validating my form with Jquery
  • ASP.NET GridView throws: The version of SQL Server in use does not support datatype 'date'
  • How to add html image in to velocity template file to send email?
  • Create an Office365 mailbox from within C# Web API method
  • Separating definition/instantiation of template classes without 'extern'
  • Creating 2d platforms using JavaScript
  • JQuery: Infinite input select
  • DataTables move rows between tables
  • calling IO Operations from thread in ruby c extension will cause ruby to hang
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • Why does Rails 3 think xE2x80x89 means â x80 x89
  • Android Library Projects on Windows and Mac
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • media foundation H264 decoder not working properly
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • JavaScript RegExp Replace