Memory alignment optimization of not only performance but also the memory size

Viewed 1117

We know that there is a difference in size of A when you define A like this:

class A 
{
  short a;
  double b;
  short c;
};

or like this

class A 
{
  short a;
  short c;
  double b;
};

I assume we are compiling for 32 bit OS and we have told compiler to align to 32 bits.

Is it really difficult for the compiler to reorder the definitions to obtain the minimal size by also achieving the same performance?

3 Answers
Related