C# unsafe code
来源:互联网 发布:实时模拟炒股软件 编辑:程序博客网 时间:2024/05/21 09:27
unsafe
{
int[] array = new int[10];
for (int i = 0; i < array.Length; i++)
{
array[i] = i;
}
fixed (int* p = array)
{
for (int i = 0; i < array.Length; i++)
{
System.Console.WriteLine(p[i]);
}
}
}
{
int[] array = new int[10];
for (int i = 0; i < array.Length; i++)
{
array[i] = i;
}
fixed (int* p = array)
{
for (int i = 0; i < array.Length; i++)
{
System.Console.WriteLine(p[i]);
}
}
}
指针在c#中是不提倡使用的,有关指针的操作被认为是不安全的(unsafe)。因此运行这段代码之前,先要改一个地方,否则编译不过无法运行。
修改方法:在右侧的solution Explorer中找到你的项目,在项目图标(绿色)上点右键,选最后一项properties,然后在Build标签页里把Allow unsafe code勾选上。之后这段代码就可以运行了,你会看到,上面这段代码可以像C语言那样用指针操纵数组。但前提是必须有fixed (int* p = array),它的意思是让p固定指向数组array,不允许改动。因为C#的自动垃圾回收机制会允许已经分配的内存在运行时进行位置调整,如果那样,p可能一开始指的是array,但后来array的位置被调整到别的位置后,p指向的就不是array了。所以要加一个fixed关键字,把它定在那里一动不动,之后的操作才有保障。
另有两点需要注意:
1)指针的使用必须放在unsafe的区域里;unsafe关键字也可作为类或方法的修饰符。
2)fixed (int* p = array)中,p的定义不能写在别处,而且fixed关键字也只能在unsafe区域里使用。
(*) 略简洁的unsafe写法
class Program
{
unsafe public static UInt16 Htons(UInt16 src)
{
UInt16 dest;
// 不能照搬C的源代码,因为有些类型长度不一样,如char(2字节),long(8字节)
// ((char*)&dest)[0] = ((char*)&src)[1];
// ((char*)&dest)[1] = ((char*)&src)[0];
((byte*)&dest)[0] = ((byte*)&src)[1];
((byte*)&dest)[1] = ((byte*)&src)[0];
return dest;
}
public static UInt16 ConciseHtons(UInt16 src)
{
UInt16 dest;
unsafe
{
((byte*)&dest)[0] = ((byte*)&src)[1];
((byte*)&dest)[1] = ((byte*)&src)[0];
}
return dest;
}
static void Main()
{
UInt16 val = 1;
// 如果方法是unsafe的,则必须在unsafe block里调用
unsafe
{
val = Htons(val);
}
Console.WriteLine(val);
// 更简洁的写法是把unsafe block写在函数内部
val = ConciseHtons(val);
Console.WriteLine(val);
}
}
{
unsafe public static UInt16 Htons(UInt16 src)
{
UInt16 dest;
// 不能照搬C的源代码,因为有些类型长度不一样,如char(2字节),long(8字节)
// ((char*)&dest)[0] = ((char*)&src)[1];
// ((char*)&dest)[1] = ((char*)&src)[0];
((byte*)&dest)[0] = ((byte*)&src)[1];
((byte*)&dest)[1] = ((byte*)&src)[0];
return dest;
}
public static UInt16 ConciseHtons(UInt16 src)
{
UInt16 dest;
unsafe
{
((byte*)&dest)[0] = ((byte*)&src)[1];
((byte*)&dest)[1] = ((byte*)&src)[0];
}
return dest;
}
static void Main()
{
UInt16 val = 1;
// 如果方法是unsafe的,则必须在unsafe block里调用
unsafe
{
val = Htons(val);
}
Console.WriteLine(val);
// 更简洁的写法是把unsafe block写在函数内部
val = ConciseHtons(val);
Console.WriteLine(val);
}
}
(*) stackalloc
stackalloc的用处仅仅是把数组分配在栈上(默认是分配在托管堆上的)。
class MyClass
{
public int val;
}
class Program
{
static void Main()
{
unsafe
{
MyClass *p = stackalloc MyClass[1]; // Error!! 如果类型要放在托管堆上则不行,如果MyClass是struct就OK了
p->val = 1;
int *iArray = stackalloc int[100]; // OK,在栈上创建数组, int类型本身就是放在栈上的
}
}
}
{
public int val;
}
class Program
{
static void Main()
{
unsafe
{
MyClass *p = stackalloc MyClass[1]; // Error!! 如果类型要放在托管堆上则不行,如果MyClass是struct就OK了
p->val = 1;
int *iArray = stackalloc int[100]; // OK,在栈上创建数组, int类型本身就是放在栈上的
}
}
}
转载地址:http://www.cnblogs.com/dc10101/archive/2009/03/06/1404586.html
0 0
- C# unsafe code
- C# unsafe code
- Using Unsafe Code in C#
- C# unsafe code(转)
- C# unsafe code(轉)
- C# -- Unsafe Code and Pointers
- All about Unsafe Code in C#
- C#基础—不安全代码(unsafe code)
- C#基础之unsafe code(不安全代码)
- 关于使用Unsafe code
- CSharp - Unsafe code
- C# 不安全代码如何可以正常编译(unsafe code may only appear if compiling with/unsafe)
- C#学习之unsafe
- c# unsafe设置
- Unsafe code may only appear if compiling with /unsafe
- Unsafe code may only appear if compiling with /unsafe
- Unsafe code may only appear if compiling with /unsafe
- Unsafe code may only appear if compiling with /unsafe
- 苹果ARM64移动处理器
- iOS中UITextField 使用全面解析
- mysql GROUP_CONCAT 函数
- phpcms 常用变量、数组
- http://bbs.chinaunix.net/thread-457730-1-1.html
- C# unsafe code
- Web前端浏览器兼容初探
- Oracle EBS 入门
- libharu(1):windows下编译方法
- zabbix-2.0.0 server & agent安装
- android开发中如何结束所有的activity
- 网页顶部空白 head解析到body中
- Testin实验室发布中国安卓游戏指数(8月报):欢乐斗地主夺冠
- Python 中list赋值引用