English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
When a code block uses unsafe When the modifier is marked, C# allows the use of pointer variables in functions.unsafe codeOr unmanaged code refers to the use ofPointerVariable code block.
Pointer is a variable whose value is the address of another variable, that is, the direct address of a memory location. 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 are examples of pointer type declarations:
Example | Description |
---|---|
int* p | p is a pointer to an integer. |
double* p | p is a pointer to a double-precision number. |
float* p | p is a pointer to a floating-point number. |
int** p | p is a pointer to a pointer to an integer. |
int*[] p | p is a one-dimensional array of pointers to integers. |
char* p | p is a pointer to a character. |
void* p | p is a pointer to an unknown type. |
When declaring multiple pointers in the same declaration, the asterisk * Written only with basic types; not used as a prefix for each pointer name. For example:
int* p1, p2, p3; // Correct int *p1, *p2, *p3; // Error
The following example illustrates the use of unsafe The use of pointers as modifiers:
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 will produce the following results:
Data is: 20 Address is: 99215364
You can also declare part of a method as unsafe code without declaring the entire method as unsafe code. The following example illustrates this.
You can use ToString() Methods retrieve data stored at the location referenced by a 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 will produce the following results:
Data is: 20 Data is: 20 Address is: 77128984
You can pass a pointer variable 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}1, var2); p.swap(x, y); Console.WriteLine("After Swap: var1:{0}, var2: {1}1, var2); Console.ReadKey(); } } }
When the above code is compiled and executed, it will produce the following results:
Before Swap: var1: 10, var2: 20 After Swap: var1: 20, var2: 10
In C#, an array name and a pointer to data of the same data type as the array 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.
Therefore, if you need to access array data using pointer variables, you can do so as we usually do in C or C++++ As done in the previous example, use fixed keyword to fix pointers.
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) /* Display the array address in the pointer */ 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} *(ptr + i)); } Console.ReadKey(); } } }
When the above code is compiled and executed, it will produce 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
To compile unsafe code, you must switch to the command line compiler specified /unsafe Command line.
For example, to compile a program named prog that contains unsafe code1Enter the command in the command line to compile a program with unsafe code named prog:
csc /unsafe prog1.cs
If you are using the Visual Studio IDE, you need to enable unmanaged code in the project properties.
The steps are as follows:
To open, double-click the Properties (Solution Explorer) node in the resource manager.Project Properties (Project Properties).
Click Build Tab
Select options"Allow unsafe code".