1.62. C# unsafe code

发布时间 : 2025-10-25 13:34:31 UTC      

Page Views: 9 views

When a code block is marked with the unsafe modifier,C# allows the use of pointer variables in functions.Unsafe or unmanaged code refers to code blocks that use pointer variables.

1.62.1. Pointer variable #

A pointer is a variable whose value is the address of another variable, thatis, the direct address of the memory location. Like other variables or constants, you must declare pointers before using pointers to store the addresses of other variables.

The general form of a pointer variable declaration is:

type* var-name; 

Here is an example of a pointer type declaration:

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 an 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 multiple pointers are declared in the same declaration, the asterisk * write only with the underlying type; it is 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 pointers whenthe unsafe modifier is used in C#:

Example #

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 also not declare the entire method as unsafe code, but only declare part of the method as unsafe code. The following example illustrates this.

1.62.2. Use pointers to retrieve data values #

You can use the ToString() method to retrieve data stored at the location referenced by the pointer variable. The following example demonstrates this:

Example #

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 

1.62.3. Pass the pointer as a parameter to the method #

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

Example #

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 

1.62.4. Use pointers to access array elements #

In C#, the array name and a pointer to the same data type as the array dataare different variable types. For example, int*p and int[] p aredifferent types. You can increase the pointer variable p, because it is notfixed in memory, but the array address is fixed in memory, so you cannot increase the array p.

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

The following example demonstrates this:

Example #

using System; namespace UnsafeCodeApplication { class TestPointer { public unsafe static void Main() { int[] list = {10, 100, 200}; fixed(int *ptr = list) /* Show array addresses in pointers */ 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 

1.62.5. Compile unsafe code #

You must switch to the /unsafe command line specified bythe command-line compiler.

For example, to compile an unsafe code named prog1.cs , which need to entera command on the command line:

csc /unsafe prog1.cs 

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

The steps are as follows:

  • Open the project properties by double-clicking the properties node in Explorer (Solution Explorer).

  • Click Build tabs.

  • Select option “Allow unsafe code”.

《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。