Does order of declaration matter?


I have recently found this article:

And I am quite surprised with what they said about structure fields order. Does it really matter? In their example, by declaring variables in other order, they saved 8 bytes. However, shouldn't compiler take care of it? Is it true, and should I declare variables more carefully?

Thanks in advance.
I think, yes because this is not Java or C# but C/C++. They have garbage collector.(Java or C#). But I may wrong and I see this for the first time too as you.
Last edited on
It's very rare that you're developing in an environment in which 8 bytes is going to make a difference.

If you're (un)lucky enough to be developing for that sort of environment, then I guess you need to worry about this sort of thing - and if you're good at it, then you can earn big bucks as a specialist :)

For most of us, though, it's not worth worrying about.
structure fields are aligned to byte boundaries, i think msvc aligns to 8 byte boundaries by default. this means that every field starts at a memory address which is an 8 byte boundary.

see #pragma pack for more info.
Topic archived. No new replies allowed.