-1

In my impression, I seem read something like "the property in a class should be declared to fit something(like 8 byte)", the idea is something like that:

class A{
    int a;
    int b;
    float c;
};

instead of

class A{
    int a;
    float c;
    int b;
};

because 2 int together can fit 8 byte.

But I forgot where I got the idea, I even forgot the main idea about it, is that reasonable?

TryinHard
  • 4,078
  • 3
  • 28
  • 54
ggrr
  • 7,737
  • 5
  • 31
  • 53

1 Answers1

0

The concept that you're looking for is struct padding (putting space between elements of a struct) or struct packing (squeezing the spaces together). I'm sure you can google it.

The basic answer though is yes, these structs might be sized differently. If the float needs to be aligned to 8 bytes for performance and the ints on this platform are 4 bytes each, then you'll end up with 4 bytes being wasted in the second layout. A 4 byte gap is left between a and c to align c to 8 bytes. But all that is dependent on your compiler and platform to decide.

Ewan Mellor
  • 6,747
  • 1
  • 24
  • 39