fixed syntax is similar to using , it is only intended to pin (pin) an object on the heap and get a pointer to this object. Docking is necessary because the object can be moved by the garbage collector and the pointer obtained earlier will not be valid. After exiting the fixed block, the object becomes available to the collector again.
Example:
int kol = 5; int*[] a = new int*[kol]; int[] some = new int[kol]; fixed (int *p = some) { for (int i = 0; i < kol; ++i) { some[i] = i + 1; // Элементы массива располагаются в памяти непрерывно // Как и в С, sizeof(int) при сложении учитывается a[i] = p + i; Console.WriteLine("Address: 0x{0}, Value: {1}", new IntPtr(a[i]).ToString("X"), *a[i]); } }
Result:
Address: 0xFF1E0D99C8, Value: 1 Address: 0xFF1E0D99CC, Value: 2 Address: 0xFF1E0D99D0, Value: 3 Address: 0xFF1E0D99D4, Value: 4 Address: 0xFF1E0D99D8, Value: 5
You mentioned that you want to create a class in which there will be a field with an array of pointers. Here you need to be careful, because you have to keep some array fixed for at least as long as the array of pointers to it will be used (which is more likely - as long as an instance of the class exists, especially if some passed to it from somewhere outside) . If it is difficult to do this through fixed , then use GCHandle.Alloc with type GCHandleType.Pinned .
a[i] = &some[i];- so does not work? - Denis Bubnov