连续的内存存储误解in.net?误解、内存、net、in

2023-09-04 11:31:26 作者:舍身入魔

http://msdn.microsoft.com /en-us/library/ms379570(v=vs.80).aspx

我知道阵列 .NET 保存在连续时尚。 (在MEM)

我也知道,名单,其中; ..> 不是。 (嗯,不是所有的列表类型...看到我的第2'的问题)

在这里,我有2个qustions

我知道后4,8,16 ......插入项目名单 - 名单重新分配本身在内存

我也知道,我可以给他发了容量的构造函数,以便让他知道在什么大小我还是要建立他(减少重新分配)。

现在的问题是为什么?他不不存储本身连续,所以为什么他关心重 分配自己? (他不必须寻找自由和放大器;连续的内存单元)

为什么清单与结构被分配在连续的内存,不像类的列表?

解决方案

名单,其中,T> 做存储内存连续。在内部,它使用一个阵列,用于它的存储。当你到达的能力,一个新的数组分配和存储器复制。

这是真正的名单,其中,T> 的类或结构的实例。然而,当 T 是引用类型(类),你存储的参考文献的连续列表的。该类的实例不能是连续的,你可以包括其中包含100引用的相同的实例的类的列表。

因此​​,要解决您的具体问题:

  

现在的问题是为什么呢?他不不存储本身连续,所以为什么他关心重新分配自己的?

它的确实的存储项目连续,这就是为什么重新分配是必需的。

  

为什么与结构所列出的被分配在连续的内存,不像类的列表?

两者都连续存储的,但在第二种情况下(类)你存储引用的列表的一类实例

操作系统 基本分页存储 连续存储方式的改进

http://msdn.microsoft.com/en-us/library/ms379570(v=vs.80).aspx

I know that Arrays in .net is stored in a contiguous fashion. ( in mem)

I also know that List<..> is not. ( well...not all list types... see at my 2'nd question)

From here I have 2 qustions

I know that after 4,8,16... inserted items to a list - the list reallocate itself in memory.

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).

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<T> does store memory contiguously. 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. However, when T is a reference type (class), you're storing a contiguous list of references. The class instances cannot be contiguous as you can include a list which contains 100 references to the same instance of a class.

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?

Both are stored contiguously, but in the second case (classes) you're storing a list of references to a class instance.