In C, if I have the following struct Data:
struct __attribute__((__packed__)) Data
{
double x;
int y;
};
And then create an array named val of this struct:
Data val[3];
All values stored in val (i.e. val[0].x, val[0].y, val[1].x, val[1].y, val[2].x and val[2].y are contiguous in memory.
Now, when having the following struct in C#:
[StructLayout(LayoutKind.Sequential, Pack=0)]
struct Data
{
public double x;
public int y;
}
And then create an array named val of this struct:
Data val[3];
I would expect that all values stored in val are contiguous in memory, which they are not (based on the tests that I have conducted). In details, val[0].x and val[0].y are both contiguous, but they are not contiguous with val[1] (which, in turn, is not contiguous with val[2]).
My question is: in C#, how can I create a variable (that is an array) of a certain struct that all its elements and members are contiguous in memory?
fixedpointer to&val[0]?Pack=0means "default padding", and what you are looking at is packing at the least possible. That'd bePack=1in C# I believePack=1can cause issues (speed and/or stability). Are you doing the for managed/native interop reasons, or something else?