Does order of declaration matter?

Hello,

I have recently found this article:

http://en.wikibooks.org/wiki/Optimizing_C%2B%2B/Code_optimization/Faster_operations

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. http://www.cplusplus.com/forum/general/14659/
Topic archived. No new replies allowed.