当前位置:网站首页>了解下C# 不安全代码
了解下C# 不安全代码
2022-08-02 08:17:00 【nginx】

指针变量指针 是值为另一个变量的地址的变量,即,内存位置的直接地址。就像其他变量或常量,您必须在使用指针存储其他变量地址之前声明指针。
指针变量声明的一般形式为:
下面是指针类型声明的实例:
type* var-name;
实例描述int* pp 是指向整数的指针。double* pp 是指向双精度数的指针。float* pp 是指向浮点数的指针。int** pp 是指向整数的指针的指针。int*[] pp 是指向整数的指针的一维数组。char* pp 是指向字符的指针。void* pp 是指向未知类型的指针。在同一个声明中声明多个指针时,星号 * 仅与基础类型一起写入;而不是用作每个指针名称的前缀。 例如:
下面的实例说明了 C# 中使用了 unsafe 修饰符时指针的使用:
int* p1, p2, p3; // 正确int *p1, *p2, *p3; // 错误
实例
当上面的代码被编译和执行时,它会产生下列结果:
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();}}}
您也可以不用声明整个方法作为不安全代码,只需要声明方法的一部分作为不安全代码。下面的实例说明了这点。
Data is: 20Address is: 99215364
使用指针检索数据值您可以使用 ToString() 方法检索存储在指针变量所引用位置的数据。下面的实例演示了这点:
实例
当上面的代码被编译和执行时,它会产生下列结果:
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();}}}
传递指针作为方法的参数您可以向方法传递指针变量作为方法的参数。下面的实例说明了这点:
Data is: 20Data is: 20Address is: 77128984
实例
当上面的代码被编译和执行时,它会产生下列结果:
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();}}}
使用指针访问数组元素在 C# 中,数组名称和一个指向与数组数据具有相同数据类型的指针是不同的变量类型。例如,int *p 和 int[] p 是不同的类型。您可以增加指针变量 p,因为它在内存中不是固定的,但是数组地址在内存中是固定的,所以您不能增加数组 p。
Before Swap: var1: 10, var2: 20After Swap: var1: 20, var2: 10
因此,如果您需要使用指针变量访问数组数据,可以像我们通常在 C 或 C++ 中所做的那样,使用 fixed 关键字来固定指针。
下面的实例演示了这点:
实例
当上面的代码被编译和执行时,它会产生下列结果:
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();}}}
编译不安全代码为了编译不安全代码,您必须切换到行编译器指定 /unsafe 行。
Address of list[0] = 31627168Value of list[0] = 10Address of list[1] = 31627172Value of list[1] = 100Address of list[2] = 31627176Value of list[2] = 200
例如,为了编译包含不安全代码的名为 prog1.cs 的程序,需在命令行中输入命令:
如果您使用的是 Visual Studio IDE,那么您需要在项目属性中启用不安全代码。
csc /unsafe prog1.cs
步骤如下:
- 通过双击资源管理器(Solution Explorer)中的属性(properties)节点,打开项目属性(project properties)。
- 点击 Build 标签页。
- 选择选项"Allow unsafe code"。
原文来自:
边栏推荐
猜你喜欢

Postman download localization of installation and use

BGP solves routing black hole through MPLS

Write a small game in C (three chess)

血气方刚的年轻小伙竟去做家政小哥,是怎样成功逆袭转行的

etcd implements large-scale service governance application combat

52.【bool类型输入任何非0数值不为1的版本原因】

PostgreSQL learning summary (11) - PostgreSQL commonly used high-availability cluster solutions

location对象,navigator对象,history对象学习

Mysql Mac版下载安装教程

C语言基础_共用体
随机推荐
How to use postman
Biotin-EDA|CAS:111790-37-5| Ethylenediamine biotin
A young man with strong blood and energy actually became a housekeeper. How did he successfully turn around and change careers?
Codeforces Round #811 (Div. 3)无DF
MFC最详细入门教程[转载]
A little bit of knowledge - why do not usually cook with copper pots
图扑软件数字孪生油气管道站,搭建油气运输管控平台
Redisson的看门狗机制
Flink 系统性学习笔记系列
小说里的编程 【连载之二十四】元宇宙里月亮弯弯
Analysis of software testing technology How far is Turing test from us
pnpm的安装与使用
R language plotly visualization: use the plotly visualization model to predict the true positive rate (True positive) TPR and false positive rate (False positive) FPR curve under different thresholds
Write a small game in C (three chess)
[OC学习笔记]Block三种类型
知识点滴 - 为什么一般不用铜锅做菜
MySQL读写分离与主从延迟
QT web development - Notes - 3
C语言_条件编译
积分商城商品供应商选择的三个要求