惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
T
Threatpost
C
Cybersecurity and Infrastructure Security Agency CISA
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
Intezer
C
Cyber Attacks, Cyber Crime and Cyber Security
The Register - Security
The Register - Security
量子位
Security Latest
Security Latest
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
MyScale Blog
MyScale Blog
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
Spread Privacy
Spread Privacy
Jina AI
Jina AI
博客园 - 【当耐特】
P
Palo Alto Networks Blog
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
G
GRAHAM CLULEY
宝玉的分享
宝玉的分享
Hacker News - Newest:
Hacker News - Newest: "LLM"
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
有赞技术团队
有赞技术团队
T
Tor Project blog
H
Hacker News: Front Page
A
Arctic Wolf
NISL@THU
NISL@THU
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
Engineering at Meta
Engineering at Meta
V
V2EX
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
I
InfoQ
D
Docker
L
LINUX DO - 最新话题
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
U
Unit 42

博客园 - 山峰旺旺

博文阅读密码验证 - 博客园 2019年春节第一天上班 FastDFS搭建文件系统(单机版) 博文阅读密码验证 - 博客园 CentOS7 下docker 部署 Asp.Net Core Linux (CentOS7.0)安装Asp.Net Core项目总结 Linux 下 安装 .Net Core(CentOS 7) Spring框架IOC容器和AOP解析(转) 文本相似度simhash算法 Intellij Idea 2017.3版本MAVEN项目打JAR包 java 敏感词过滤 (DFA算法)(转) intellij idea 修改背景保护色&&修改字体&&快捷键大全(转) C#队列Queue实现一个简单的电商网站秒杀程序 2018年目标和愿景 CDN原理(转,学习用) 面试理论整理 C#中的where泛型约束中的new()使用(转) C#中重写(override)和覆盖(new)的区别 (备注:转,留自己用) 2015年总结
C# unsafe(fixed) 介于 managed code & unmanaged code之间的特性(转)
山峰旺旺 · 2016-06-07 · via 博客园 - 山峰旺旺

1. unsafe在C#程序中的使用场合:

1)实时应用,采用指针来提高性能;

2)引用非.net DLL提供的如C++编写的外部函数,需要指针来传递该函数;

3)调试,用以检测程序在运行过程中的内存使用状况。

2. 使用unsafe的利弊

好处是:性能和灵活性提高;可以调用其他dll的函数,提高了兼容性;可以得到内存地址;

带来麻烦是:非法修改了某些变量;内存泄漏。

3. unsafe与unmanaged的区别

managed code是在CLR监管下运行的程序。以下任务由CLR来执行:管理对象内存,类型安全检测和冗余处理。从另一方面来说,unmanaged code也就是能由程序员直接进行内存操作的程序。而unsafe是介于managed和unmanaged之间的桥梁,它使得managed code也能使用指针来控制和操作内存。

4. unsafe的使用

unsafe可以用来修饰类、类的成员函数、类的全局变量,但不能用来修饰类成员函数内的局部变量。编译带有unsafe代码的程序也要在“configuration properties>build” 中把允许unsafe代码设为真。

但是在managed code中使用unsafe时也要注意,正因为CLR可以操作内存对象,假如你写了一下代码:

      public unsafe void add(int *p)       {           *p=*p+4;       }

p的地址值可能会在运行过程中被CLR所修改,这通常可采用fixed来处理,使指针所指向的地址不能被改变。如下:

      fixed(int *p=& value)         {             add(p);         }

转自:http://blog.csdn.net/wolf_baby/article/details/755944