Struct with fixed-length array member - is it possible?

  • Thread starter Thread starter Rafal Gwizdala
  • Start date Start date
R

Rafal Gwizdala

Hello,


Can you provide some hints on how would you solve a problem like this? I'm
out of ideas...
Here's the problem: Let's imagine a C structure containing a fixed-size
array

struct XYZ
{
int V1;
int V2;
double V3[50];
};

This structure, when allocated, occupies a contiguous block of memory (408
bytes in my compiler).
Now I would like to have the same structure in C#, such that it would have
THE SAME MEMORY LAYOUT as the C structure. The memory size does not need to
be identical as in C, but the structure should occupy a single memory block
and all data, including the array, should be placed in this block.

There is no possibility in C# to declare a fixed-size array and to 'embed'
it completely in a struct. When I make a declaration like this:

public struct XYZ
{
int V1;
int V2;
double[] V3;
}

the V3 member is a reference to array object, not the array itself.
Can you think of any way to get the C/C++ layout?

Best Regards,
Rafal Gwizdala
 
Rafal Gwizdala said:
Hello,


Can you provide some hints on how would you solve a problem like this? I'm
out of ideas...
Here's the problem: Let's imagine a C structure containing a fixed-size
array

struct XYZ
{
int V1;
int V2;
double V3[50];
};

This structure, when allocated, occupies a contiguous block of memory (408
bytes in my compiler).
Now I would like to have the same structure in C#, such that it would have
THE SAME MEMORY LAYOUT as the C structure. The memory size does not need
to be identical as in C, but the structure should occupy a single memory
block and all data, including the array, should be placed in this block.

There is no possibility in C# to declare a fixed-size array and to 'embed'
it completely in a struct. When I make a declaration like this:

public struct XYZ
{
int V1;
int V2;
double[] V3;
}

2.0 offers this, finally, using the fixed keyword


public stuct XYZ
{
fixed double V3[50];
}

This requires unsafe code, however.
 
Back
Top