I know that an instance of this C++ class :
class A {
char c;
int iiii;
short ss;
};
would look kind of like this in memory : c| | | |i|i|i|i|s|s| | |
This 4/2 letter annotation has no sense (But I think that my point is clear)
1 byte for char
, 3 bytes of padding
, 4 bytes of int
, 2 bytes for the short
, and 2 bytes of tail padding
(platform dependant, but it won't change the logic)
From C++ standards (Compilers wouldn't change the order of the fields in my example):
Nonstatic data members of a (non-union) class with the same access control (Clause 11) are allocated so that later members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (Clause 11). Implementation alignment requirements might cause two adjacent members not to be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and virtual base classes (10.1).
so, I would like to know if it is the same for Java classes, Can the compiler change the order to reduce the padding ?