当前位置:网站首页>了解下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: 20
Address 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] = 10
Address of list[1] = 31627172
Value of list[1] = 100
Address of list[2] = 31627176
Value of list[2] = 200
例如,为了编译包含不安全代码的名为 prog1.cs 的程序,需在命令行中输入命令:
如果您使用的是 Visual Studio IDE,那么您需要在项目属性中启用不安全代码。
csc /unsafe prog1.cs
步骤如下:
- 通过双击资源管理器(Solution Explorer)中的属性(properties)节点,打开项目属性(project properties)。
- 点击 Build 标签页。
- 选择选项"Allow unsafe code"。
原文来自:
边栏推荐
猜你喜欢
文章解读 -- FlowNet3D:Learning Scene Flow in 3D Point Clouds
Application and case analysis of CASA model and CENTURY model
USACO美国信息学奥赛竞赛12月份开赛,中国学生备赛指南
如何建立私域流量?私域流量对企业有什么好处?
unity pdg 设置隐藏不需要的节点以及实现自动勾选自动加载项
【论文阅读】Distilling the Knowledge in a Neural Network
Redisson的看门狗机制
PostgreSQL learning summary (11) - PostgreSQL commonly used high-availability cluster solutions
C语言_指针
二分类和多分类
随机推荐
QT web development - Notes - 3
BGP solves routing black hole through MPLS
CASA模型、CENTURY模型应用与案例分析
如何建立私域流量?私域流量对企业有什么好处?
Mysql Mac版下载安装教程
etcd implements large-scale service governance application combat
PyCharm usage tutorial (detailed version - graphic and text combination)
Biotin-EDA|CAS:111790-37-5| 乙二胺生物素
What attributes and methods are available for page directives in JSP pages?
商业智能平台BI 商业智能分析平台 如何选择合适的商业智能平台BI
mysqldump --set-gtid-purged=OFF
52.【bool类型输入任何非0数值不为1的版本原因】
工程师如何对待开源 --- 一个老工程师的肺腑之言
抓包工具Charles修改Response步骤
MySQL读写分离与主从延迟
etcd实现大规模服务治理应用实战
AttributeError: module ‘clr‘ has no attribute ‘AddReference‘
R语言plotly可视化:plotly可视化回归模型实际值和回归预测值的散点图分析回归模型的预测效能、一个好的模型大部分的散点在对角线附近(predicted vs actual)
高仿【华为消费者业务官网】和精彩动画剖析:练习在低代码平台中嵌入JS代码
MySQL 中 count() 和 count(1) 有什么区别?哪个性能最好?