Unsafe code: copy

image_pdfimage_print

/*
C# Programming Tips & Techniques
by Charles Wright, Kris Jamsa

Publisher: Osborne/McGraw-Hill (December 28, 2001)
ISBN: 0072193794
*/
namespace nsType
{
using System;

public class IntCopy
{
public static void Main ()
{
int [] arr1 = new int[] {1, 4, 2, 8, 5, 7};
int [] arr2 = new int[arr1.Length];
MemCpy (arr2, arr1, arr1.Length);
arr1[0] = 142857;
for (int x = 0; x < arr1.Length; ++x) { Console.Write ("arr1[{0}] = {1} ", x, arr1[x]); Console.Write ("arr2[{0}] = {1} ", x, arr2[x]); } } static unsafe public void MemCpy (int [] dst, int [] src, int size) { if ((size > dst.Length) || (size > src.Length))
{
ArgumentException e = new ArgumentException(“The size argument is too large for one of the array arguments”);
throw (e);
}
fixed (int *Src = src, Dst = dst)
{
int* pSrc = Src;
int* pDst = Dst;
for (int n = 0; n < size; ++n) { *pDst++ = *pSrc++; } } } } } [/csharp]

Multiple Indirect

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


using System; 
 
public class MultipleIndirect { 
  unsafe public static void Main() { 
    int x;    // holds a int value  
    int* p;  // holds an int pointer 
    int** q; // holds an pointer to an int pointer 
 
    x = 10; 
    p = &amp;x; // put address of x into p 
    q = &amp;p; // put address of p into q 
 
    Console.WriteLine(**q); // display the value of x  
  } 
}


           
          


Use fixed to get a pointer to the start of a string

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


// Use fixed to get a pointer to the start of a string. 
 
using System; 
 
public class FixedString { 
  unsafe public static void Main() { 
    string str = "this is a test"; 
 
    // Point p to start of str. 
    fixed(char* p = str) { 
 
      // Display the contents of str via p. 
      for(int i=0; p[i] != 0; i++) 
        Console.Write(p[i]); 
    } 
 
    Console.WriteLine(); 
     
  } 
}


           
          


Index a pointer as if it were an array

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Index a pointer as if it were an array.

using System;

public class PtrIndexDemo {
unsafe public static void Main() {
int[] nums = new int[10];

// index pointer
Console.WriteLine(“Index pointer like array.”);
fixed (int* p = nums) {
for(int i=0; i < 10; i++) p[i] = i; // index pointer like array for(int i=0; i < 10; i++) Console.WriteLine("p[{0}]: {1} ", i, p[i]); } // use pointer arithmetic Console.WriteLine(" Use pointer arithmetic."); fixed (int* p = nums) { for(int i=0; i < 10; i++) *(p+i) = i; // use pointer arithmetic for(int i=0; i < 10; i++) Console.WriteLine("*(p+{0}): {1} ", i, *(p+i)); } } } [/csharp]

An array name with an index yields a pointer to the start of the array

image_pdfimage_print

   

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/


/* An array name with an index yields a pointer to the 
   start of the array. */ 
 
using System; 
 
public class PtrArray { 
  unsafe public static void Main() { 
    int[] nums = new int[10]; 
 
    fixed(int* p = &amp;nums[0], p2 = nums) { 
      if(p == p2) 
        Console.WriteLine("p and p2 point to same address."); 
    } 
  } 
}


           
          


Demonstrate pointer comparison

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Demonstrate pointer comparison.

using System;

public class PtrCompDemo {
unsafe public static void Main() {

int[] nums = new int[11];
int x;

// find the middle
fixed (int* start = &nums[0]) {
fixed(int* end = &nums[nums.Length-1]) {
for(x=0; start+x <= end-x; x++) ; } } Console.WriteLine("Middle element is " + x); } } [/csharp]

Demonstrate the effects of pointer arithmethic

image_pdfimage_print

/*
C#: The Complete Reference
by Herbert Schildt

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/

// Demonstrate the effects of pointer arithmethic.

using System;

public class PtrArithDemo {
unsafe public static void Main() {
int x;
int i;
double d;

int* ip = &i;
double* fp = &d;

Console.WriteLine(“int double
“);

for(x=0; x < 10; x++) { Console.WriteLine((uint) (ip) + " " + (uint) (fp)); ip++; fp++; } } } [/csharp]