类型转换时的堆栈行为 [英] Stack behavior while type-casting

查看:77
本文介绍了类型转换时的堆栈行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当将对象(整数)强制转换为数据类型(整数)时,有人能解释一下堆栈和堆的行为吗?

任何有思想的想法都将受到欢迎..


问候
拉杰夫


P.S.正在将其发布到此处,因为它是基于场景的查询.老实说,我没有用谷歌搜索这个,因为我知道这是一个CP级问题.

Can anyone explain me the behaviour of Stack and Heap when an object(Integer) is type casted as a data type(int)?

Any thoughtful ideas would be welcome..


Regards
Rajeev


P.S. Am posting it here, as it is a scenario-based query. Honestly,I didn''t google for this one , as I was aware that this is a CP - level question.

推荐答案

首先,这不是完全类型转换,即使这些概念是相关的.这称为装箱和拆箱,请参见 http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx [ ^ ].

.NET中所谓的类型转换是将一种类型解释为另一种类型.由于没有数据移动到任何地方,因此它与堆栈或堆无关.对于堆栈,直到更改堆栈帧,堆栈指针什么都不会发生. .NET中的这种常规"类型转换在某种程度上类似于C ++重新解释转换".

在C#中装箱时,将进行堆栈分配,因为需要一些内存来保存System.Object后代的实例,例如as System.Int32.取消装箱没有任何反应.最终,垃圾收集器将回收堆内存.首先,应用程序域中的任何其他可访问对象都应无法访问该对象. GC稍后根据GC行为收集无法访问的对象的内存.

—SA
First, this is not exactly type casting, even though those concepts are related. This is called boxing and unboxing, see http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx[^].

What is called type casting in .NET is interpreting of one type as it was some another type. As no data is moved anywhere, it has nothing to do with stack or heap. As to stack, nothing happens to the stack pointer at all until stack frame is changed. This "normal" type casting in .NET is somewhat similar to C++ "reinterpret cast".

At to boxing in C#, stack allocation takes place, as some memory is need to hold an instance of System.Object descendant, such as System.Int32. Nothing happens on unboxing. Heap memory is eventually reclaimed by the Garbage Collector. First, the object should become inaccessible by any other accessible object in the Application Domain. GC collects memory of the inaccessible object later, depending on GC behavior.

—SA


这篇关于类型转换时的堆栈行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆