0

i have a 3D array and i want to get IntPtr to point on it so here what i did ..can anyone tell me if it's right or not...

fixed (Int16* mypointer = &myvolume[0, 0, 0])
{
     //then i cast mypointer as IntPtr
}

notice that myvolume is of dimensions 200 x 100 x 100

Aamir
  • 14,882
  • 6
  • 45
  • 69
Sara S.
  • 1,365
  • 1
  • 15
  • 33

1 Answers1

3

Are you sure that what you really want is an unsafe pointer? Using pointers like this is very very rare in C# land.

Eric Lippert recently wrote about a simple class that offers some of the behavior of pointers, without resorting to unsafe code. You might consider extending it to work with your multidimensional arrays.

Chris Pitman
  • 12,990
  • 3
  • 41
  • 56
  • THanks Chris ,actually i'm using some vtk algorithms that requires a pointer to my volume that's why i'm using pointers, i'll check this article, Thanks for your effort – Sara S. Mar 15 '11 at 13:15
  • It's not as rare as you'd think. Pointer arithmetic is by far the fastest way to iterate over arrays like this and you'll find a lot of this kind of code in performance critical apps. – MattDavey Mar 15 '11 at 13:41
  • @MattDavey I agree there are places for it, and have used it myself for image manipulation routines. The danger is that programmers new to "high performance" computing in C# will jump right to pointers without trying/profiling the standard C# solutions. – Chris Pitman Mar 15 '11 at 13:53
  • actually i used it before with another SDKs but i was troubleshooting a problem so i wanted to make sure if i use it appropriately :) – Sara S. Mar 15 '11 at 14:14