0

This is what i understood :

During virtual inheritance :virtual base{} derived class along with inherited data members have to keep vpointer(to keep track of members of base to keep single instance of members) and one vpointer per inheritance hierarchy that virtually inherits base are present in derived class.

In this code with this knowledge:

  • sizeof(B) and sizeof(C) then should be = sizeof(A) + vptr
  • sizeof(D) should be = sizeof(A) + vptr_from_B + vptr_from_C
struct A{
    int i=3,j=1,k=2;
};
struct B: virtual A{};
struct C: virtual A{};
struct D:B,C{};

int main()
{
    std::cout<<"size of class D = "<<sizeof(D)<<"\n";
    std::cout<<"size of class C = "<<sizeof(C)<<"\n";
    std::cout<<"size of class B = "<<sizeof(B)<<"\n";
    std::cout<<"size of class A = "<<sizeof(A)<<"\n";

    return 0;
}

Case1 :

struct A{}; empty struct

D = 16 , C = 8 , B = 8 , A = 1

Case2 :

struct A{ int i = 3; }; one integer

D = 24 , C = 16 , B = 16 , A = 4

  • why B ,C and D have extra 4 bytes ?

Case3 :

struct A{ int i = 3, j = 1; }; two integers

D = 24 , C = 16 , B = 16 , A = 8

  • now B, C and D remains of same size even though sizeof A increases by 4 bytes,...?

Case4 :

struct A{ int i = 3, j = 1, k = 2}; three integers

D = 32 , C = 24 , B = 24 , A = 12

  • like Case1 we have again 4 bytes extra ?

Pattern continues. . . It feels like some sort of zero padding to keep size of virtually inherited Derived classes multiple of 8, i have no idea what exactly is going on and why ?

sorry for stupid question in advance

Eurus_
  • 41
  • 4
  • 4
    You should not rely on predictable class sizes. Their storage is implementation specific. Any questions "why" should be considered as meanless. – 273K Apr 09 '21 at 18:17
  • 1
    [Data structure padding](https://stackoverflow.com/questions/6025269/data-structure-padding) is a common practice to keep objects aligned to processor architecture word sizes. – Nathan Pierson Apr 09 '21 at 18:24
  • Padding is necessary to keep the 8-byte pointer aligned to 8 when objects are stored in an array. – Hans Passant Apr 09 '21 at 18:25
  • There is padding and alignment which are an implementation detail which may or may not depend on performance or may be a hardware requirement. There should be many duplicates talking about these. – drescherjm Apr 09 '21 at 18:28
  • 2
    Your feeling is correct. On your platform, pointers appear to need or prefer to be 8-byte aligned. Possibly they would be fine if unaligned (albeit at a performance penalty), or possibly not (segv crash). C++ tends to prefer performance, even if it would cause a little padding in objects. – Eljay Apr 09 '21 at 18:31
  • it seems, its better to keep things in array inside struct to avoid padding without getting offsets misaligned, right ? – Eurus_ Apr 10 '21 at 13:27

1 Answers1

0

( Good Reads = this on why Memory alignment is important, this on why its a big deal for CPUs ).

  • In short : to perform performance critical algorithms, some CPUs need aligned data(otherwise there will be segv or some exception error) and if its not aligned then they have to perform few extra cycles to fetch the correct offset to access the data which gives quite a performance hit in long run.

In Case2:

padd just enough to so that offset be divisible by its corresponding sizeof(data_member)

struct B & C looks like :
enter image description here next offset is set to 16, size is = 16 bytes

struct D looks like :
enter image description here next offset is set to 16, size is = 24 bytes

  • Important Note : order of storage does not matter, final size will be the same, as n = max(sizeof(data_members)), final offset should be divisible by n, here n = 8. If in Case2: in struct B&C if i first put 8(B|C::vptr) & offset = 8, then 4(int) & offset = 12, final offset will padded with 4 bytes to make him divisible by n,in the end final offset = 24.

In Case3: does not need padding because these above 4 padded bytes are settled for new integer member

In Case4

struct D looks like :
enter image description here next offset is set to 16, size is = 32 bytes

Eurus_
  • 41
  • 4
  • why Table in answer is not appearing ? it was appearing when i was writing the answer and during edit too, but in final answer only " | : -- " symbols are present ?? – Eurus_ Apr 10 '21 at 14:53