Today’s article looks at the IL2CPP and C++ compiler output for a variety of C# language features. Do you want to know what happens when you use them? Read on to find out!
Posts Tagged sizeof
I continue to learn a lot by reading the C++ code that IL2CPP outputs. Like reading decompiled code, it gives some insight into what what Unity’s build process is doing with the C# we give it. This week I learned that sizeof(MyStruct)
isn’t a compile-time constant like it is in C++. Because of that, IL2CPP generates some less-than-ideal C++ code every time you use it. Today’s article shows the process I went through to work around that issue and ends up with some code you can drop into your project to avoid the problem.
Structs can be a great way to keep the garbage collector off your back and to use the CPU’s data cache more effectively. Not everything can be a struct though. At a minimum, you’ll need to use some Unity and .NET classes like MonoBehaviour
and string
. If your struct has any of these as fields, you can no longer use sizeof(MyStruct)
. That really limits its usefulness, so a workaround is needed. Enter object handles: a simple way to represent any object
as a plain old int
which won’t break sizeof
. Read on to see how these work and some code you can easily drop into your project to start using them right away!