1

The code that I've written:

#include <iostream>

using std::cout;
using std::endl;

struct S
{
    long l;
};

struct A
{
    int a;
};

struct B : A, S{ };

int main()
{
    cout << "sizeof(A) = " << sizeof(A) << endl; //4
    cout << "sizeof(S) = " << sizeof(S) << endl; //8
    cout << "sizeof(B) = " << sizeof(B) << endl; //16 != 4 + 8
}

demo

Why do we have to allocate an additional 4 bytes for B? How this additional memory is used?

  • Related: [See this question](http://stackoverflow.com/questions/119123/why-isnt-sizeof-for-a-struct-equal-to-the-sum-of-sizeof-of-each-member/119128#119128). Some of the answers are very well-formed. – WhozCraig Aug 23 '14 at 07:06
  • @WhozCraig Not sure if it's 'related'. Seems like a duplicate. – Rapptz Aug 23 '14 at 07:07
  • 1
    @Rapptz it really isn't. Stacking in class derivation is one thing *not* particularly covered in those answers. The ideas of layout, alignment, and padding, are nicely covered, however. – WhozCraig Aug 23 '14 at 07:09
  • possible dupe: http://stackoverflow.com/questions/8813756/inheritance-and-size-of-object – Rapptz Aug 23 '14 at 07:10

3 Answers3

0

For POD data type, there can be padding and alignments.

Non-maskable Interrupt
  • 3,841
  • 1
  • 19
  • 26
0

In B's memory layout, int comes first, then long. The compiler likely inserts 4 bytes of padding between them to 8-align the long assuming any instance of the structure will also have its starting address 8-aligned.

Similar things would happen if you put an A and a S instance on your stack: the compiler would leave 4 bytes empty in between.

See Data Structure Alignment.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
0

Memory image of a B instance:

int  a; // bytes 0-3
long l; // bytes 4-11

Problem:

l is an 8-byte variable, but its address is not aligned to 8 bytes. As a result, unless the underlying HW architecture supports unaligned load/store operations, the compiler cannot generate correct assembly code.

Solution:

The compiler adds a 4-byte padding after variable a, in order to align variable l to an 8-byte address.

Memory image of a B instance:

int  a; // bytes 0-3
int  p; // bytes 4-7
long l; // bytes 8-15
barak manos
  • 29,648
  • 10
  • 62
  • 114
  • Problem? There's no problem here. – Benjamin Lindley Aug 23 '14 at 07:27
  • The compiler has no trouble generating correct assembly code. It inserts padding into the structs on its own, you don't need to do it manually. The OP's question is a demonstration of this. – Benjamin Lindley Aug 23 '14 at 07:30
  • @BenjaminLindley: Who said you need to do it manually??? The answer explains how the compiler solves this problem!!! – barak manos Aug 23 '14 at 07:31
  • Well, it doesn't sound like that's what you're saying. You say, "the compiler cannot generate correct assembly code", then you provide a solution which sounds like an instruction to the reader, not an explanation of what the compiler does. – Benjamin Lindley Aug 23 '14 at 07:33
  • @BenjaminLindley: I think that since the question doesn't refer to any runtime error, it's pretty obvious that the answer explains how a given problem is solved (rather than stating that there is a problem in generating compiled code). The model of the answer - Description, Problem, Solution, Description - is simply there to depict the situation. – barak manos Aug 23 '14 at 07:37
  • @BenjaminLindley: Nevertheless, using the word "Add" might have been slightly misleading here, so I changed it to "The compiler adds"... – barak manos Aug 23 '14 at 07:45
  • You answer seems logical for me. Accepted. –  Aug 23 '14 at 13:57