[ Team LiB ] |
Recipe 16.6 Creating and Using an Array of PointersProblemYou need to create, initialize, and use an array containing pointers. SolutionThe following code creates three pointers (TheNewBrush1, TheNewBrush2, and TheNewBrush3) that are inserted as elements in an array. The array of pointers to the NewBrush structure is created and set to a size of 3 so that it can hold each NewBrush structure. This newly defined array now contains undefined pointers. These undefined pointers should be initialized either to point to a value or to point to null. Here, all of the pointers in the array are initialized as null pointers. Finally, each NewBrush structure is then added to this array. Now we have a fully initialized array of pointers. From here we can use this array as we wish: unsafe { NewBrush theNewBrush1 = new NewBrush( ); NewBrush theNewBrush2 = new NewBrush( ); NewBrush theNewBrush3 = new NewBrush( ); NewBrush*[] arrayOfNewBrushPtrs = new NewBrush*[3]; for (int counter = 0; counter < 3; counter++) { arrayOfNewBrushPtrs[counter] = null; } arrayOfNewBrushPtrs[0] = &theNewBrush1; arrayOfNewBrushPtrs[1] = &theNewBrush2; arrayOfNewBrushPtrs[2] = &theNewBrush3; } Notice that the for loop initializes each pointer in the array to null before the array is used. This is usually a good practice so that you do not inadvertently use an uninitialized pointer. Using a pointer that points to null results in a NullReferenceException being thrown on current versions of the CLR. This device makes it easier to track down pointer problems. Using this newly created array of pointers to NewBrush objects allows you to use a pointer to a pointer. The following code shows how to dereference each pointer within the array arrayOfNewBrushPtrs: unsafe { fixed(NewBrush** ptrArrayOfNewBrushPtrs = arrayOfNewBrushPtrs) { for (int counter = 0; counter < 3; counter++) { ptrArrayOfNewBrushPtrs[counter]->BrushType = counter; Console.WriteLine(ptrArrayOfNewBrushPtrs[counter]->BrushType); Console.WriteLine((int)ptrArrayOfNewBrushPtrs[counter]); } } } The for loop initializes the BrushType field of each of the pointers to NewBrush objects in the array. This field is initialized to the current value of the loop counter (counter). The next two lines display this newly initialized field and the address of where the structure is located in memory. This code displays the following output: 0 1243292 1 1243284 2 1243276 DiscussionWhen using an array of pointers, the fixed statement pins the array in memory. Even though this array consists of pointers to value types, the array itself is created on the managed heap. Notice that ptrArrayOfNewBrushPtrs is defined as a pointer to a pointer. This stems from our creation of a pointer (ptrArrayOfNewBrushPtrs) that will initially point to the first element in an array of pointers. Therefore, to be able to dereference this pointer to get to the value that the pointer in the array is pointing to, we must dereference it once to get to the pointer in the array and then a second time to get the value that the array pointer is pointing to. The NewBrush structure used here is defined like this: public struct NewBrush { public int BrushType; } See AlsoSee the "Unsafe Code Tutorial" in the MSDN documentation. |
[ Team LiB ] |