Alex Rivera | Logout

List<T> to T[] without copying

Asked 2011-02-11T19:02:54.920
44

I'm have a large list of value types that needs to be given to OpenGL. It would be great if this could happen as quickly as possible. What I'm doing now looks like this:

List<Vertex> VList = new List<Vertex>();
... //Add vertices
Vertex[] VArray;
VList.CopyTo(VArray, VList.Length);
GL.SetData(..., VArray);

This list is easily 10MB big, so copying is slow. Can I do this without copying, like somehow get a pointer to the array used internally by List?

Or do I have to implement my own List class..

EDIT: I forgot to mention that I don't know the number of elements that will be added to the List.

Edit
Report

1 Answer

0

You might be able to get a pointer out of a generic List, but I wouldn't recommend it and it probably wouldn't work the way you'd expect (if at all). Basically it means getting a pointer to an object, not a memory structure like an array.

I think you should go about this the other way around, and if you need speed then work directly on a byte array using structure array pointer in an unsafe context instead.

Background info:
"Even when used with the unsafe keyword, taking the address of a managed object, getting the size of a managed object, or declaring a pointer to a managed type is not allowed." - From C#: convert generic pointer to array

MSDN unsafe

answered 2011-02-11T19:13:03.143

Your Answer