fixed

M

Martijn Mulder

I try to encapsulate a fixed array in a class, but the only thing I know so
far is that this code compiles. Does it give access to a fixed array? What
are the gotcha's?


//class FixedArray
class FixedArray
{

//data bytes
byte[] bytes;

//constructor
public FixedArray(int a)
{
bytes=new byte[a];
}

//property IntPtr
unsafe public System.IntPtr IntPtr
{
get
{
System.IntPtr a;
fixed( byte * b = bytes )
{
a=(System.IntPtr)b;
}
return a;
}
}
}


//class EntryPoint
class EntryPoint
{

//method Main
static void Main()
{
FixedArray fixedarray=new FixedArray(512);
System.IntPtr intptr=fixedarray.IntPtr;
//does intptr point to a fixed array?
}
}
 
K

KH

Does it give access to a fixed array?

No, the array is only fixed in the scope of the fixed statement:

byte[] bytes = new byte[0];

// bytes is not fixed

fixed (byte[] f = bytes)
{
// the array is fixed
}

// the array is no longer fixed

HTH
 
M

Martijn Mulder

Does it give access to a fixed array?
No, the array is only fixed in the scope of the fixed statement:

How can I get a fixed block of memory for the duration of the program? It
seems that 'fixed' memory is block-scoped and cannot be used outside it's
own block. Or is there a way, with 'static' perhaps, to keep a block of
memory away from the managed environment?
 

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