c++ - does order of members of objects of a class have any impact on performance? -


may order of members in binary architecture of objects of class somehow have impact on performance of applications use class? , i'm wondering how decide order of members of pods in case answer yes since programmer defines order of members via order of declaraions

absolutely. c++ guarantees order of objects in memory same order of declaration, unless access qualifier intervenes.

objects directly adjacent more on same cacheline, 1 memory access fetch them both (or flush both cache). cache effectiveness may improved proportion of useful data inside may higher. put, spatial locality in code translates spatial locality performance.

also, jerry notes in comments, order may affect amount of padding. sort members decreasing size, decreasing alignment (usually treat array 1 element of type, , member struct most-aligned member). unnecessary padding may increase total size of structure, leading higher memory traffic.

c++03 §9/12:

nonstatic data members of (non-union) class declared without intervening access-specifier allocated later members have higher addresses within class object. order of allocation of nonstatic data members separated access-specifier unspecified (11.1). implementation alignment requirements might cause 2 adjacent members not allocated after each other; might requirements space managing virtual functions (10.3) , virtual base classes (10.1).


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -