- Home /
Does variable order affect class size due to byte alignment in C#/Unity
I've studied recently that in C++ the order of your variables can ultimately affect the memory of your class due to byte alignment. Is the same true for our classes in C#?
class memInefficient{
int i1; //It's safer to put the 2 bool vars at the end?
bool b1;
bool b2;
int i2;
}
Although it probably happens in C# too, as with C++ it's not at all a problem. If you really care about the small portion of memory that gets "wasted" just try to group all the variables of the same type together, $$anonymous$$imilazing those gaps.
Where did you read this? I've never heard of anything of the sort :s
As far as I'm aware, bools are just glorified ints.
Either way, if this was this case, I would be absolutely shocked if the compiler didn't do this for you.
@Tarlius It's definitely true in C++ in Visual Studio 2010. You can try by making a class and then checking it's size with sizeof(myClass).
It was more of a question of "does it happen" as opposed to "does it matter." I suppose I'll just have to check and see if it is affected by the alignment. I suppose my question was more of "Does Unity have any suggestions for variable ordering?"
Googled it and found some supporting evidence. Pretty shocked, never heard of this before! I guess it makes sense in c++ though since you can do weird things like stepping over classes with offsets from the pointer to the class if I recall correctly. For that to work the compiler would have to be outputting "defined" class structures. Optimising them would break any code doing that :/
As for how this is in unity, if anyone checks it out I'd be interested in results, although I think for most cases it'd be better focusing on readability. Genuinely curious whether the compiler would do this for you though. I believe that one of the supposed benefits of JIT code is that it can be optimised for the platform its running on at run time, and alignment looks like it would be viable candidate. Since you can't know the size of the primitives until runtime, and c# doesn't let you use "real" pointers (although theres an unsafe mode I've no experience with) its possible c# could do this behind the scenes...
Although in the inspector all public/serialised variables are displayed in the same order as the code, so maybe not... :o
Answer by AndyMartin458 · Nov 13, 2013 at 09:07 PM
I created a few structs with different organization of the variables. It appears to me that the arrangement does not affect the struct when I used Marshal.SizeOf(). It really is a letdown that I can't use sizeof on a class. It is nearly certain that C# takes care of the variable memory alignment for you.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Variables inside class not showing up in Inspector C#. 2 Answers
How to refer to object based on value of a variable 1 Answer
How do you profile memory usage during compilation? 0 Answers