B
Bob Bryan
I am writing an app that uses static arrays in a base class that I only want
to define once and shared amongst all instances of that class. On my
laptop, that is using Visual Studio 2008 SP1 and running Vista, the
following code has a problem:
unsafe public partial class Test1
{
protected static int[] PowersOf2 = new int[12] { 1, 2, 4, 8, 16, 32, 64,
128, 256, 512, 1024, 2048 };
protected static int* pPowersOf2; // Pointer to the PowersOf2 array.
protected static GCHandle gchPowersOf2; // Handle to GCHandle object
used to pin the PowersOf2 array.
public Test1()
{
fixed (int* p = PowersOf2)
{
gchPowersOf2 = GCHandle.Alloc(PowersOf2, GCHandleType.Pinned);
IntPtr pAddr = Marshal.UnsafeAddrOfPinnedArrayElement(PowersOf2,
0);
pPowersOf2 = (int*)pAddr.ToPointer();
pPowersOf2 = p;
int* p2 = p;
}
}
}
When the first pPowersOf2 pointer assignment is tried, the address is not
set correctly. There is no exception thrown or any other kind of odd
behaviour. The 2nd assignment statement of pPowersOf2 also does not work
and is set to the same odd address as was returned by the ToPointer
function. I then tried setting it to p, which is set correctly and p2 is
set to the correct address. If I remove the static keyword from the
pPowersOf2 declaration, then it works perfectly. As a workaround, I can use
non-static pointers in my class - its a little extra code, however, I would
prefer to use a static pointer.
So, the question is - am I doing something wrong here, or is this a bug? I
hope someone from MS or an experienced MVP can provide some guidance.
Thanks,
Bob Bryan
to define once and shared amongst all instances of that class. On my
laptop, that is using Visual Studio 2008 SP1 and running Vista, the
following code has a problem:
unsafe public partial class Test1
{
protected static int[] PowersOf2 = new int[12] { 1, 2, 4, 8, 16, 32, 64,
128, 256, 512, 1024, 2048 };
protected static int* pPowersOf2; // Pointer to the PowersOf2 array.
protected static GCHandle gchPowersOf2; // Handle to GCHandle object
used to pin the PowersOf2 array.
public Test1()
{
fixed (int* p = PowersOf2)
{
gchPowersOf2 = GCHandle.Alloc(PowersOf2, GCHandleType.Pinned);
IntPtr pAddr = Marshal.UnsafeAddrOfPinnedArrayElement(PowersOf2,
0);
pPowersOf2 = (int*)pAddr.ToPointer();
pPowersOf2 = p;
int* p2 = p;
}
}
}
When the first pPowersOf2 pointer assignment is tried, the address is not
set correctly. There is no exception thrown or any other kind of odd
behaviour. The 2nd assignment statement of pPowersOf2 also does not work
and is set to the same odd address as was returned by the ToPointer
function. I then tried setting it to p, which is set correctly and p2 is
set to the correct address. If I remove the static keyword from the
pPowersOf2 declaration, then it works perfectly. As a workaround, I can use
non-static pointers in my class - its a little extra code, however, I would
prefer to use a static pointer.
So, the question is - am I doing something wrong here, or is this a bug? I
hope someone from MS or an experienced MVP can provide some guidance.
Thanks,
Bob Bryan