We use struct in C# whenever possible mainly because it is stored on the stack and no objects are created for it. This boosts the performance.
On the other hand, arrays are stored on the heap.
My question is, if I include an array as an element of the struct, something as follows:
struct MotionVector
{
int[] a;
int b;
}
Then what will be the consequences. Will that array be stored on stack? Or the performance advantage of using struct will be lost?
If you don’t want to create elements dynamically, consider to create a (big) buffer of MotionVector instances during startup and reuse those when needed. Then you will not get the penalty of creating/destructing them dynammically.
Of course you have to write some small functions to get a ‘free’ instance and to obtain one, use a boolean in the struct for that (or by using an interface).
To do this you could e.g.:
Create during initialisation of your app the motionvectors:
Add a boolean to the MotionVector class:
Define the new class MotionVectors: