Got a question for you all. Many of you will answer ‘it makes no difference’ but I’m somewhat adamant there is a difference due to the wording of the question:
‘Which class uses the least amount of memory?’
They’re asking for a singular answer – there must be one! I’ve had a chat with a previous Microsoft intern and he’s adamant there is no answer.
Maybe some would care to offer their thoughts?
Which class uses the least amount of memory?
class A
{
float duration;
char code;
int count;
bool enabled;
};
class B
{
float duration;
int count;
char code;
bool enabled;
};
class C
{
char code;
int count;
bool enabled;
float duration;
};
class D
{
bool enabled;
float duration;
char code;
int count;
};
On MSVC2010 I get
A = 16 bytes
B = 12 bytes
C = 16 bytes
D = 16 bytes
this is probably due to alignment of the different types. Since char and bool are both small they share a DWORD.
However this is compiler and platform independent. There is nothing stopping a compiler implementer from making them all the same size or making a char 64 bytes or whatever. The requirements of the standard are quite lax about this kind of thing.