To maintain type security, pointers are not supported by default in C#, but if the unsafe keyword is used to modify members of a class or class, such members of a class or class will be considered unsafe code. C# allows pointer variables to be used in unsafe code. In the Common Language Runtime (CLR), unsafe code refers to code that cannot be verified. Unsafe code is not necessarily dangerous, but the CLR cannot verify the security of the code. Therefore, CLR will only execute unsafe code contained in the trusted assembly.
Pointer variable
In C#, a pointer is also a variable, but its value is the memory address of another variable. Before using a pointer, we also need to declare the pointer first. The syntax format of the declared pointer is as follows:
type* var_name;
Some examples of defining pointers are listed in the following table:
Example | illustrate |
---|---|
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 |
The same as declaring variables, we can also declare multiple pointers at the same time in a single line of code, as shown below:
int* p1, p2, p3; // definition p1、p2、p3 Three integer pointers
Note: Pointer types cannot be inherited from objects, and boxing and unboxing do not support pointers, but different pointer types and between pointers and integers can be converted.
[Example] The following example demonstrates the use of unsafe keywords and pointers in C#:
using System; namespace { class Demo { static unsafe void Main(string[] args) { double f = 3.1415; double* p = &f; ("The content of the data is: {0} ", f); ("The address of the data in memory is: {0}", (int)p); (); } } }
The operation results are as follows:
The content of the string is: 3.1415
The address of the string in memory is: 11530344
Tip: When compiling the above code, you need to add -unsafe to the compile command, such as csc -unsafe.
Use pointers to retrieve the value of the data
In C#, we can use ToString() to get the value of the data pointed to by the pointer variable, as shown in the following example:
using System; namespace { class Demo { public static void Main() { unsafe { int var = 123456; int* p = &var; ("variable var The value of: {0} " , var); ("pointer p 指向The value of: {0} " , p->ToString()); ("pointer p The value of: {0} " , (int)p); } (); } } }
The operation results are as follows:
The value of the variable var is: 123456
The value pointed to by pointer p is: 123456
The value of pointer p is: 13889084
Passing a pointer as an argument to a function
We can pass pointer variables as arguments to the function as examples below:
using System; namespace { class Demo { public unsafe void swap(int* p, int *q) { int temp = *p; *p = *q; *q = temp; } public unsafe static void Main() { Demo p = new Demo(); int var1 = 10; int var2 = 20; int* x = &var1; int* y = &var2; ("Call Swap Before function: var1:{0}, var2: {1}", var1, var2); (x, y); ("Call Swap After the function: var1:{0}, var2: {1}", var1, var2); (); } } }
The operation results are as follows:
Before calling the Swap function: var1:10, var2: 20
After calling the Swap function: var1:20, var2: 10
Accessing array elements using pointers
In C#, arrays and pointers to the array and the same name as the array are different data types, such as int*p and int[]p are different data types. You can increase the value of the pointer variable p because it is not fixed in memory, but the array address is fixed in memory, so you cannot increase the value of the array p. If you need to access array data using pointer variables, you can use the fixed keyword to fix the pointer, just like we did in C or C++. Here is an example to demonstrate:
using System; namespace { class Demo { public unsafe static void Main() { int[] list = {10, 100, 200}; fixed(int *ptr = list) /* Display array address in pointer */ for ( int i = 0; i < 3; i++) { ("list[{0}] The memory address is:{1}",i,(int)(ptr + i)); ("list[{0}] The value of:{1}", i, *(ptr + i)); } (); } } }
The operation results are as follows:
The memory address of list[0] is: 51981272
The value of list[0] is: 10
The memory address of list[1] is: 51981276
The value of list[1] is: 100
The memory address of list[2] is: 51981280
The value of list[2] is: 200
Compile unsafe code
In order to compile unsafe code, the unsafe command must be used at compile time. For example, the command to compile a program containing unsafe code is as follows:
csc /unsafe
or
csc -unsafe
If you are using Visual Studio, you need to enable unsafe code in your project properties. The steps are as follows:
- Open project properties by double-clicking the properties node in the Solution Explorer;
- Click the Build tab;
- Select the option "Allow unsafe code".
This is the end of this article about the implementation of C# pointer variables and unsafe. For more related C# pointer variables and unsafe content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!