Recipe19.6.Creating and Using an Array of Pointers


Recipe 19.6. Creating and Using an Array of Pointers

Problem

You need to create, initialize, and use an array containing pointers.

Solution

The following code creates three pointers to a NewBrush structure (theNewBrush1, theNewBrush2, and theNewBrush3) that are inserted as elements in an array. The NewBrush structure used here is defined like this:

 public struct NewBrush {     public int BrushType; } 

The array of pointers is created and set to a size of 3 so that it can hold each element. 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 added to the array. Now you have a fully initialized array of pointers. From here you can use this array as you wish:

 unsafe {     NewBrush theNewBrush1 = new NewBrush( );     NewBrush theNewBrush2 = new NewBrush( );     NewBrush theNewBrush3 = new NewBrush( );     NewBrush*[] arrayOfNewBrushPtrs = new NewBrush*[3];     arrayOfNewBrushPtrs[0] = &theNewBrush1;     arrayOfNewBrushPtrs[1] = &theNewBrush2;     arrayOfNewBrushPtrs[2] = &theNewBrush3; } 

This array of pointers to NewBrush objects must be referenced as a pointer to a pointer in unsafe code. 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 the structure in memory. This code displays the following output (note that the addresses will be different on different machines):

 0 1243292 1 1243284 2 1243276 

Discussion

When 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 having created a pointer (ptrArrayOfNewBrushPtrs) that initially points to the first element in an array of pointers. To be able to dereference this pointer to get to the value that the array element is pointing to, you must dereference it once to get to the array element and then a second time to get the value that the element is pointing to.

See Also

See the "Unsafe Code Tutorial" topic in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net