Isnt the compiler outputting the wrong size for a CCandyBox? Shouldnt it be 28, it inherits 24 bytes from Cbox and 4 bytes for its pointer member. Where does the other 4 bytes come from?
Tried to do some research, I think its about the compiler aligning memory addresses or something.
Microsoft's compiler allocates 8-bytes for each of the "doubles" of "CBox" which are aligned to an 8-byte boundary, and 4-bytes for the pointer data-member of "CCandyBox" which is aligned to a 4-byte boundary.
The additional 4-bytes are the padding bytes used to align the pointer data-member of "CBox" to the three "doubles" of "CCandyBox", giving you a total of 32-bytes. If, however, your application was 64-bit, the resulting size of "CCandyBox" would be the same.
By "align", I was referring to the placement or arrangement of data to some boundary. A "boundary" is an address divisible by some even number such as 8, 16, 32, 64, and so on.
So it basically reads 8 bytes as a WHOLE rather then each as one in order to make it faster right? and if the address isnt a multiple of 8 its less efficent for some reason I dont understand lol
With modern processors, the CPU can access both even and uneven addresses. In general, accessing an even address is faster than accessing an uneven address.
Some data-types need to be aligned to a specific boundary. Let's assume we have a single "double" which is 8-bytes in length, which we will call "X".
If "X" is aligned to an 8-byte boundary, the CPU would be able to scoop up all of the bytes of "X" in one go. However, if "X" was not aligned to an 8-byte boundary, the CPU would most likely have to make two trips to memory to obtain all the bytes relating to "X" -- accessing memory is a time-consuming process, and to make an additional trip to memory is unnecessary workload.
In addition, accessing an uneven address adds to the CPU's workload further. Note that on some, older processors, accessing an uneven address could cause an hardware exception to be thrown which may not be taken too lightly by the operating system.
So basically if "X" was not aligned and half of it was in one 8-byte boundry and the rest was in another the CPU would have to make two trips to get all the memory?
Any type other than char (and signedchar, unsignedchar) may have an implementation-defined alignment requirement.
> The largest data in the class is how big the boundary will be right?
No.
The alignment of a class object would be governed by the most stringent alignment requirement of its data members (including anonymous base class objects and hidden data members)
Also I already read that article and the example you showed, doesn't that example go with what I said?
The largest data type in the class determines its alignment?
If they did have implementation defined alignments would that mean that the sizeof operator may not reveal the correct size? So I doubt any compiler would want that to happen.
Also isn't the alignment set by the processor? Not by the compiler? So how does the compiler control this?
> doesn't that example go with what I said?
> The largest data type in the class determines its alignment?
No.
For instance, examine class C struct C { A a ; B b ; };
The member a has a size of 103, and has no alignment requirement (1).
The member b has a (smaller) size of 16, but has (a more stringent) alignment requirement of 4.
sizeof(C) >= sizeof(A) + sizeof(B)
alignment of(C) == 4 (which is maximum of (more stringent of) ( alignment of(A), alignment of(B) ) )
> would that mean that the sizeof operator may not reveal the correct size?
The sizeof operator will always yield the correct size (the number of bytes an object of that type occupies).
std::alignment_of<> will always yield the correct alignment requirement for an object of that type.
However, the alignment requirement for an object with a smaller size may be more stringent (have a larger value) than that of an object with a bigger size. Size and alignment are not dependant on each other; they are orthogonal. An array of a million char char[1000000] has a big size, and also has the least stringent alignment requirement. An int may have a much smaller size than the 1MB array, but may (usually does) have a more stringent alignment requirement.
> Also isn't the alignment set by the processor? Not by the compiler?
> So how does the compiler control this?
The alignment is defined by the implementation (compiler) based on what is required by (or is more optimal) for the particular target processor architecture.
For instance, the alignment requirements defined by GCC for i386/Windows would be different from the alignment requirements defined by GCC for mips64.
In a base class with all its members either private or protected, that will not be changed if its derived class inherits via public right, at least in the derived class?
But if in the base class all its members are public, this can be changed if the if its derived class inherits via protected or private right, at least in the derived class?