数组中的内存分配? [英] memory allocation in arrays?

查看:80
本文介绍了数组中的内存分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们使用数组在.net中分配内存时,它是动态的还是静态的?

我认为它是静态的,但是..是否有可能由CLR管理(使之动态).??

When we are allocating memory in .net with array,is it dynamic or static??

i am thinking it is static ,but.. is there any possibility to manage by the CLR(making dynamic).. ??

推荐答案

不是静态的.该数组是一个引用对象,因此它被分配在堆上(并由垃圾收集器控制).与其他高级.NET语言相比,使用C ++/CLI非常不寻常,还可以将其存储在堆栈中或作为静态内存.从这一刻起,让我们忘记C ++/CLI和手动编码的IL,然后回到C#.

内存分配没有分为静态和动态"两种.三种方式存储对象:在为静态数据保留的内存区域中,在堆栈上和在堆上.现在,如果您使用像数组这样的引用对象,则实际上有两个单独的对象:引用和始终在堆上的引用对象本身.

如果您将引用视为对象,则可以将其存储在任何位置:可以是静态,堆栈或堆对象.如果是静态字段,则为静态;否则为false.如果这是局部变量,则在堆栈上.当引用是某物的非静态成员时,会出现其他选项.在这种情况下,此引用成员的存储取决于其声明类型的实例的存储方式.递归地重复这些注意事项,您将发现每种情况下的存储方式.同样,被引用的对象本身不依赖于引用的存储方式:它在堆上.

托管引用(如果是非常特殊的对象).在大多数情况下,您可以将其视为指针,但这不是指针:可以重定位被引用的对象,但该引用保持有效.如果引用在应用程序域中正在运行的代码中是 reachable ,则不能对被引用的对象进行垃圾回收.请参阅:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29 [ ^ ].

—SA
Not static. The array is a reference object so it is allocated on heap (and is controlled by the Garbage Collector). With C++/CLI, which is very unusual compared with other high-level .NET languages, it''s also possible to store it on stack or as a static memory. From this moment, let''s forget about C++/CLI and manually-coded IL and get back to C#.

Memory allocation is not classified into static and "dynamic". There are three ways to store an object: in a memory area reserved for static data, on stack and on heap. Now, if you work with a reference object like an array, there are actually two separate objects: a reference and the referenced object itself which is always on heap.

If you consider a reference as an object, if can be stored anywhere: can be static, stack or heap object. It is static if it is a static field; it''s on stack if this is a local variable. The other options comes when the reference is a non-static member of something. In this case, the storage of this reference member depends on how the instance of its declaring type is stored. Repeat these considerations recursively, and you will find out how things are stored in each particular case. Again, the referenced object itself does not depend on how reference is stored: it goes on heap.

The managed reference if a very special object. In most cased, you can consider it as a pointer, but this is not a pointer: a referenced object can be relocated, but the reference is maintained valid. If a reference is reachable from the running code in the application domain, the referenced object cannot be garbage-collected. Please see:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

—SA


.Net中的数组是对象,并且以几乎所有其他内容相同的方式进行分配,因此它们是垃圾回收的,可以在以下位置构造运行时等.我认为这在您的意思上是动态的".但是,您不能调整数组的大小.也许您正在寻找List< T>?
Arrays in .Net are objects and allocated in the same way as pretty much everything else, so they''re garbage collected, can be constructed at runtime, etc. I think this is ''dynamic'' in the sense you mean it. However, you can''t resize an array; maybe you''re looking for List<T>?


这篇关于数组中的内存分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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