C/C++中的指针是一种非常灵活而强大的引用机制,但同时也非常脆弱,稍有不慎,就会出错。
Java完全摈弃了指针的概念,而代之以对象引用(object reference),基本上消灭了由指针而导致的错误。
C#借鉴了Java的作法,引入了对象引用的概念,但同时,不像Java做的那么绝,仍然提供了指针的概念。
CLR支持三种类型的指针:
1) 托管指针(managed pointers)
2) 非托管指针(unmanaged pointers)
3) 非托管函数指针(unmanaged function pointers)
托管指针是一种新类型的指针,指向托管堆中的内存区。
非托管指针是传统的C/C++指针,指向非托管内存区。
非托管函数指针也是传统的C/C++指针,指向函数地址。
C#中的指针属于非托管指针,在C#中不能直接使用托管指针,但作为 by-ref 参数传递机制的 ref 和 out 机制就是利用托管指针实现的。
C#代码:
using System;
public class Test
{
public static void Main()
{
int n = 20;
int m;
compute(n, out m);
}
private static void compute(int x, out int y)
{
y = x * 2;
}
}
编译以后,生成如下 IL 代码(为了简单起见,这里只取compute()方法的 IL 代码):
.method private hidebysig static void  compute(int32 x, [out] int32& y) cil managed {   // Code size       7 (0x7)   .maxstack  8   IL_0000:  nop   IL_0001:  ldarg.1   IL_0002:  ldarg.0   IL_0003:  ldc.i4.2   IL_0004:  mul   IL_0005:  stind.i4   IL_0006:  ret } // end of method Test::compute
从compute()的 IL 代码即可明显看出,参数 y 的类型即是托管指针 int32&。
stind.i4 指令将栈顶的 32 位整数值存储到由次栈顶元素(即参数 y)所指示的内存地址中。
上面的C#代码用的是 out 指示符,如果换成 ref 指示符,生成的 IL 代码是一样的,所不同的是,C#编译器会检查 m 的赋值情况,在执行 compute(n, ref m) 之前,m 必须被明确赋值(definite assigned),而对于 out 指示符,m 不需要赋值。
托管指针和非托管指针的区别是明显的,托管指针指向的是托管堆中的地址,而非托管指针指向的是非托管内存中的地址。
那么,对象引用与托管指针又有什么不同呢?
从物理实现的角度看,对象引用也是一种指针,而且是一种托管类型的指针。与托管指针不同,对象引用只能指向对象的起始位置,而不能指向对象内部,也就是说,对象引用指向的是对象的整体,而非局部。
而托管指针一般是指向对象内部某个成员的地址。除此之外,托管指针还可以指向求值栈(evaluation stack)中的位置,或静态变量,甚至非托管内存区。

转载于:https://www.cnblogs.com/prowyh/archive/2012/03/15/2399514.html

对象引用与托管指针(object references and managed pointers)相关推荐

  1. 关于object references an unsaved transient instance - save the transient instance before flushing的解决方法

    关于object references an unsaved transient instance - save the transient instance before flushing的解决方法 ...

  2. 解决object references an unsaved transient instance - save the transient instance before flushing 的错误

    异常信息: Caused by: org.hibernate.TransientPropertyValueException: object references an unsaved transie ...

  3. Hibernate中的级联策略和object references an unsaved transient instance - save the transient instance before

    本片博文整理关于Hibernate中级联策略cascade和它导致的异常: Exception in thread "main" org.hibernate.TransientOb ...

  4. object references an unsaved transient instance【异常】

    object references an unsaved transient instance[异常] 参考文章: (1)object references an unsaved transient ...

  5. object references an unsaved transient instance - save the transient instance before flushing .

    http://blog.csdn.net/zdwzzu2006/article/details/7552234 异常1:not-null property references a null or t ...

  6. object references an unsaved transient instance save the transient instance before flushing

    问题内容: 在一个hibernate项目中,为一个Model类添加一个多对一的配置 当表单中选择数据时对其多对一对象进行保存操作,没有选择的话则不对其做任何操作,如下面代码:当Attach不为空的情况 ...

  7. object references an unsaved transient instance - save the transient instance before flushing

    刚开是在写项目的时候,去掉了很多jar包所以才会出现这个问题 Struts Problem Report Struts has detected an unhandled exception: Mes ...

  8. object references an unsaved transient instance - save the transient instance before flushing异常问题处理

    object references an unsaved transient instance - save the transient instance before flushing异常问题处理 ...

  9. Exception in thread main org.hibernate.TransientObjectException: object references an unsaved tran

    今天在使用一对多,多对一保存数据的时候出现了这个错误 Hibernate错误: Exception in thread "main" org.hibernate.Transient ...

最新文章

  1. 关于Silverlight中多项目共享DLL文件的讨论
  2. C++环境测试CPU是否支持MMX,SSE-未完
  3. IDEA对新建java线程池的建议
  4. 'unsigned char'-C编程中的声明,赋值和用法
  5. 前端学习(2672): vue3.0脚手架路由改变
  6. python数据格式化后导入数据库_如何为数据库导入重新格式化这个json?
  7. matlab将图片旋转的代码_【MATLAB】钟表
  8. 程序员告诉你如何用技术手段玩转冲顶大会
  9. Java常用的几个Json库
  10. 这是一份简易的ACM模板
  11. CentOS操作系统的22个log日志
  12. Euraka使用入门
  13. office visio连接线控制方法
  14. 密码分析(一):差分密码分析
  15. 医学信息化管理与建设
  16. 在 Vue3 成为默认版本后,盘点了 Vue3 与 Vue2 的区别
  17. SAP 散装物料作用及设置
  18. c++ 读写 .flo 文件
  19. 所属技术领域技术人员的知识和能力
  20. 淘集集怎么打单发货?易掌柜怎么打印淘集集订单?

热门文章

  1. 直接型FIR滤波器的fpga实现
  2. java soa例子_哪位大牛能举个实例讲下SOA与传统架构的区别?
  3. STM32串口第一个字节丢失问题的分析过程
  4. 使用特异性特征提取网络辅助分类任务
  5. 如何用node命令和webpack命令传递参数 转载
  6. Java_数据交换_Jackson_用法入门
  7. Java学习lesson 14
  8. Python----Day1
  9. Block介绍(二)内存管理与其他特性
  10. 一键生成HTML4和WAP站