[英]Contiguous memory storing misunderstanding in.net?
http://msdn.microsoft.com/en-us/library/ms379570(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/ms379570(v=vs.80).aspx
I know that Arrays
in .net
is stored in a contiguous fashion. 我知道
.net
中的Arrays
是以连续的方式存储的。 ( in mem) (在mem中)
I also know that List<..>
is not. 我也知道
List<..>
不是。 ( well...not all list types... see at my 2'nd question) (好吧......不是所有列表类型......请参阅我的第2个问题)
From here I have 2 qustions 从这里我有2个问题
I know that after 4,8,16... inserted items to a list - the list reallocate
itself in memory. 我知道在4,8,16 ...插入项目到列表后 - 列表在内存中
reallocate
自己。
I also know I can send him the Capacity
in ctor in order to let him know in what size im gonna create him ( to reduce re-allocation). 我也知道我可以向他发送
Capacity
,以便让他知道我将创建他的大小(以减少重新分配)。
The question is why ? 问题是为什么 ? he Doesn NOT store itself contiguously , so why does he care about re- allocating himself ?
他不会连续存放自己,为什么他关心重新分配自己呢? ( he doesnt have to find free & contiguous memory cells)
(他不必找到免费和连续的存储单元)
why Does List with structs is allocated in contiguous memory, unlike a List of classes ? 为什么带有结构的List是否在连续内存中分配,与类列表不同?
List<T>
does store memory contiguously. List<T>
会连续存储内存。 Internally, it uses an array for its storage. 在内部,它使用数组进行存储。 When you reach the capacity, a new array is allocated and the memory is copied.
达到容量后,将分配新阵列并复制内存。
This is true for List<T>
instances of classes or structs. 对于类或结构的
List<T>
实例,这是正确的。 However, when T
is a reference type (class), you're storing a contiguous list of references . 但是,当
T
是引用类型(类)时,您将存储一个连续的引用列表 。 The class instances cannot be contiguous as you can include a list which contains 100 references to the same instance of a class. 类实例不能是连续的,因为您可以包含一个列表,该列表包含对同一个类实例的100个引用。
As such, to address your specific questions: 因此,要解决您的具体问题:
The question is why ?
问题是为什么? he Doesn NOT store itself contiguously , so why does he care about re- allocating himself ?
他不会连续存放自己,为什么他关心重新分配自己呢?
It does store the items contiguously, which is why the reallocation is required. 它确实连续存储项目,这就是需要重新分配的原因。
why Does List with structs is allocated in contiguous memory, unlike a List of classes?
为什么带有结构的List是否在连续内存中分配,与类列表不同?
Both are stored contiguously, but in the second case (classes) you're storing a list of references to a class instance. 两者都是连续存储的,但在第二种情况(类)中,您存储了对类实例的引用列表。
1) all the object references are still stored contigously 1)所有对象引用仍然是有条不紊地存储的
2) a list och classes still must let the objects be stored like ordinary objects. 2)列表och类仍然必须让对象像普通对象一样存储。 thay cannot be forced into a contigious array - but the references to them can
不能强迫进入一个有名的阵列 - 但对他们的引用可以
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.