Allocating a continuous block of memory for a structure?

  • Thread starter Thread starter Kourosh
  • Start date Start date
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?
 
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.
 
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

Back
Top