Latest web development tutorials

C # unsafe code

When a block of code using theunsafe modifier tag, C # allows the use of pointer variable in the function.Unsafe code or unmanaged code is the use of a code blockpointervariable.

Pointer variable

A pointer is a variable value of the address of another variable, namely, the direct address of the memory location.Like other variables or constants, you must use the pointer before other variable storage address pointer declaration.

The general form of a pointer variable declaration is:

type * var-name;

The following is a valid pointer declaration:

int * ip; / * points to an integer * /
double * dp; / * pointer to a double-precision number * /
float * fp; / * pointer to a float * /
char * ch / * pointer to a char * /

The following example illustrates the use of the C # usingunsafe modifier when the pointer:

using System;
namespace UnsafeCodeApplication
{
    class Program
    {
        static unsafe void Main (string [] args)
        {
            int var = 20;
            int * p = & var;
            Console.WriteLine ( "Data is: {0}", var);
            Console.WriteLine ( "Address is: {0}", (int) p);
            Console.ReadKey ();
        }
    }
}

When the above code is compiled and executed, it produces the following results:

Data is: 20
Address is: 99215364

You can not declare the entire method as unsafe code, only part of the need to declare the method as unsafe code. The following example illustrates this point.

Retrieving data values ​​using a pointer

You can use theToString () method of the reference position data stored in a retrieval pointer variables.The following example illustrates this point:

using System;
namespace UnsafeCodeApplication
{
   class Program
   {
      public static void Main ()
      {
         unsafe
         {
            int var = 20;
            int * p = & var;
            Console.WriteLine ( "Data is: {0}", var);
            Console.WriteLine ( "Data is: {0}", p-> ToString ());
            Console.WriteLine ( "Address is: {0}", (int) p);
         }
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Data is: 20
Data is: 20
Address is: 77128984

Passing a pointer as a parameter method

You can pass a pointer variable as a parameter to the method of approach. The following example illustrates this point:

using System;
namespace UnsafeCodeApplication
{
   class TestPointer
   {
      public unsafe void swap (int * p, int * q)
      {
         int temp = * p;
         * P = * q;
         * Q = temp;
      }

      public unsafe static void Main ()
      {
         TestPointer p = new TestPointer ();
         int var1 = 10;
         int var2 = 20;
         int * x = & var1;
         int * y = & var2;
         
         Console.WriteLine ( "Before Swap: var1: {0}, var2: {1}", var1, var2);
         p.swap (x, y);

         Console.WriteLine ( "After Swap: var1: {0}, var2: {1}", var1, var2);
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Before Swap: var1: 10, var2: 20
After Swap: var1: 20, var2: 10

Use pointers to access array elements

In C #, the array name and a pointer to the array data with the same data type is a pointer to a different variable types. For example, int * p and int [] p is a different type. You can increase the pointer variable p, because it is not fixed in the memory, but the address of the array is fixed in memory, so you can not add an array p.

So, if you need to use the pointer variables to access the array data, as we usually do in C or C ++ as keywords to usefixed fixed pointer.

The following example illustrates this point:

using System;
namespace UnsafeCodeApplication
{
   class TestPointer
   {
      public unsafe static void Main ()
      {
         int [] list = {10, 100, 200};
         fixed (int * ptr = list)

         / * Display pointer array address * /
         for (int i = 0; i <3; i ++)
         {
            Console.WriteLine ( "Address of list [{0}] = {1}", i, (int) (ptr + i));
            Console.WriteLine ( "Value of list [{0}] = {1}", i, * (ptr + i));
         }
         Console.ReadKey ();
      }
   }
}

When the above code is compiled and executed, it produces the following results:

Address of list [0] = 31627168
Value of list [0] = 10
Address of list [1] = 31627172
Value of list [1] = 100
Address of list [2] = 31627176
Value of list [2] = 200

Compile unsafe code

In order to compile unsafe code, you must switch to the command line compiler specify the/ unsafe command line.

For example, to compile a program called prog1.cs contain unsafe code, enter the command at the command line:

csc / unsafe prog1.cs

If you are using Visual Studio IDE, you need to enable unsafe code in the project properties.

Proceed as follows:

  • By double-clicking Explorer (Solution Explorer) attributes (properties) node, open theproject properties (project properties).
  • Click theBuild tab.
  • Select the option"Allow unsafe code".