Reputation: 1829
There are various articles, for instance msdn.microsoft.com/en-us/library/ms379570(v=vs.80).aspx, which seem to suggest that the items of a generic List are stored in contiguous memory. However I can't find any definitive statement to that effect either way. Does anyone know of one. Note that it is very clearly stated that arrays are contiguous but this question applies to List.
Also is there any way to access the memory used by a List directly?
Upvotes: 3
Views: 2472
Reputation: 111870
The source of List<T> is quite explicit:
public class List<T> : IList<T>, System.Collections.IList, IReadOnlyList<T>
{
private const int _defaultCapacity = 4;
private T[] _items;
a List<T>
contains an array of T
(T[]
), so if the array is contiguous (yes, it is), then List<T>
is contiguous :-)
And from the List<T> MSDN page:
The List class is the generic equivalent of the ArrayList class. It implements the IList generic interface by using an array whose size is dynamically increased as required.
For the second question:
Also is there any way to access the memory used by a List directly?
You seem to be more a "theoretical" programmer, so the answer is no. To the "practical" programmers that are reading this, they can use:
public static class ArrayFromList
{
public static FieldInfo GetField<T>()
{
return ArrayFromListImpl<T>.Field;
}
public static T[] GetArray<T>(List<T> list)
{
return ArrayFromListImpl<T>.GetArray(list);
}
public static class ArrayFromListImpl<T>
{
public static readonly FieldInfo Field;
public static readonly Func<List<T>, T[]> GetArray;
static ArrayFromListImpl()
{
Field = typeof(List<T>)
.GetFields(BindingFlags.Instance | BindingFlags.NonPublic)
.Where(x => x.FieldType == typeof(T[]))
.Single();
var par = Expression.Parameter(typeof(List<T>));
var exp = Expression.Lambda<Func<List<T>, T[]>>(Expression.Field(par, Field), par);
GetArray = exp.Compile();
}
}
}
Use it like:
var lst = new List<int> { 1, 2, 3 };
var ptr = ArrayFromList.GetArray(lst);
Note that this won't work on a "theretical" List<T>
that "hides" the array in a sub-object :-) (but will work on .NET and Mono :-) )
Upvotes: 7