Allocating a continuous block of memory for a structure?

K

Kourosh

I have a structure that has a byte array as a field:
struct foo
{
public int A;
public int B;
public byte[] C;
}

I want to create an instance of this structure, such that all fields
A, B, and C are in a continous block of memory. Right now if I assign
C a new byte array, it gets created in a different spot in memory than
fields A and B.
Is there any way i can do this?
 
W

Willy Denoyette [MVP]

Kourosh said:
I have a structure that has a byte array as a field:
struct foo
{
public int A;
public int B;
public byte[] C;
}

I want to create an instance of this structure, such that all fields
A, B, and C are in a continous block of memory. Right now if I assign
C a new byte array, it gets created in a different spot in memory than
fields A and B.
Is there any way i can do this?



Not sure why you need this for, anyway, the only possible way is to declare
the array of fixed size in an unsafe block.

unsafe struct foo
{
public int A;
public int B;
fixed public byte[128] C; // embedded array of 128 bytes
}

Note both requirement; fixed size and unsafe.

Willy.
 
J

Jon Skeet [C# MVP]

Not sure why you need this for, anyway, the only possible way is to declare
the array of fixed size in an unsafe block.

unsafe struct foo
{
public int A;
public int B;
fixed public byte[128] C; // embedded array of 128 bytes
}

Note both requirement; fixed size and unsafe.

Also note that this is only available as of C# 2.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top