C# Unsafe Code
C# allows the use of pointer variables in functions when a block of code is marked with the unsafe modifier. Unsafe code or unmanaged code is a block of code that uses pointer variables.
Pointer Variables
A pointer is a variable whose value is the address of another variable, i.e., the direct address of a memory location. Just like other variables or constants, you must declare a pointer before using it to store the address of another variable.
The general form of pointer variable declaration is:
type *var-name;
The following is a valid pointer declaration:
int *ip; /* 指向一个整数 */ double *dp; /* 指向一个双精度数 */ float *fp; /* 指向一个浮点数 */ char *ch /* 指向一个字符 */
The following example illustrates the use of pointers when the unsafe modifier is used in C#:
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 When executed, it will produce the following results:
Data is: 20 Address is: 99215364
You don’t need to declare the entire method as unsafe code, but only a part of the method as unsafe code. The following example illustrates this point.
Retrieve data values using pointers
You can use the ToString() method to retrieve the data stored at the location referenced by the pointer variable. The following example demonstrates this:
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 method parameter
You can pass a pointer variable to a method as a method parameter. The following example illustrates this:
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
Accessing array elements using pointers
In C#, the array name and a pointer to the array data have Pointers of the same data type are different variable types. For example, int *p and int[] p are different types. You can increment the pointer variable p because it is not fixed in memory, but the array address is fixed in memory so you cannot increment the array p.
So, if you need to access array data using pointer variables, you can use fixed keyword to fix the pointer as we usually do in C or C++.
The following example demonstrates this:
using System; namespace UnsafeCodeApplication { class TestPointer { public unsafe static void Main() { int[] list = {10, 100, 200}; fixed(int *ptr = list) /* 显示指针中数组地址 */ 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
Compiling unsafe code
In order to compile unsafe code, you must switch to the command line The compiler specifies the /unsafe command line.
For example, to compile a program named prog1.cs that contains unsafe code, enter the command at the command line:
csc /unsafe prog1.cs
If you are using the Visual Studio IDE, then you need to enable unsafe code in the project properties .
The steps are as follows:
Open project properties by double-clicking the properties node in Solution Explorer.
Click on the Build tab.
Select the option "Allow unsafe code".
The above is the content of [c# tutorial] C# unsafe code. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!