I use a datastructure in my project and in the context of a paricular structure i have a doubt about strucure padding. First Look at the strucure given below. I use Visual S
While all the answers saying that it's up to the compiler are correct, because it is up to the compiler, the language specification places some limitations on the layout. These restrictions apply to all structures in C, "plain old data" in C++03 (which basically means only using features from C) and "standard-layout" structures in C++11 (which allows having constructor and destructor). The restrictions are:
The restriction to standard layout is important. Neither holds for classes that have base classes or virtual members.
Now when combined with the desire not to waste memory, this actually leaves only one practical algorithm, that is therefore used by all compilers. Which does not mean that all compilers will produce the same layout for the same input, because the sizes and alignment requirements for various primitive types differ between platforms. The algorithm is:
(I am almost certain MSDN for MSVC++ describes this somewhere, but couldn't quickly find it)
Alignment and padding for structs and classes is not specified by the standard. It's entirely down to the compiler. However, all sane compilers follow the underlying platform ABI. In your case the platform is Windows, and the Windows platform ABI is adhered to.
The padding in this case, for both structs, is after the last member. The first struct has one extra padding byte, and the second struct has three extra padding bytes.
The largest type in the struct has size 4. And that means that the overall size will be a multiple of 4. For both structs, the smallest multiple of 4 that accommodates the struct is 8.
Each data type has an alignment property. A 4 byte data type has alignment of 4. A 2 byte data type has an alignment of 2. A type with alignment of 4 is aligned when placed on a 4 byte offset from the start of the struct. A type with alignment of 2 is aligned when placed on a 2 byte offset from the start of the struct. And so on.
Members are placed at the smallest offset that respects both the order of declaration of members, and the alignment property of the members.
For an example with padding internal to the struct consider this struct
struct MyStruct
{
char c;
int i;
};
The alignment of c
is 1, and the alignment of i
is 4. So, c
is placed on a 1 byte boundary, and i
must be placed on a 4 byte boundary. That means that c
will have offset 0, then there will be 3 padding bytes, and then i
will be laid out at an offset of 4.
Where ever the compiler wants. If it matters, you're doing something non-portable anyway, so why does it matter?
check out this:
typedef struct myTagDATA_PACK3
{
char c;
double d;
int i;
}DATA_PACK3;
it shows 24 bytes. That is: double:8 bytes. int : 4 bytes + (4 bytes padding) = 8 bytes. char: 1 byte + (7 bytes padding) = 8 bytes.
Total:24 bytes.