C# huge size 2-dim arrays

Posted by 4eburek on Stack Overflow See other posts from Stack Overflow or by 4eburek
Published on 2010-04-07T15:31:59Z Indexed on 2010/04/07 15:33 UTC
Read the original article Hit count: 275

I need to declare square matrices in C# WinForms with more than 20000 items in a row. I read about 2GB .Net object size limit in 32bit and also the same case in 64bit OS. So as I understood the single answer - is using unsafe code or separate library built withing C++ compiler.

The problem for me is worth because ushort[20000,20000] is smaller then 2GB but actually I cannot allocate even 700MB of memory. My limit is 650MB and I don't understand why - I have 32bit WinXP with 3GB of memory. I tried to use Marshal.AllocHGlobal(700<<20) but it throws OutOfMemoryException, GC.GetTotalMemory returns 4.5MB before trying to allocate memory.

I found only that many people say use unsafe code but I cannot find example of how to declare 2-dim array in heap (any stack can't keep so huge amount of data) and how to work with it using pointers. Is it pure C++ code inside of unsafe{} brackets?

Could you please provide a small example of working with matrices using pointers in unsafe code.

© Stack Overflow or respective owner

Related posts about c#

Related posts about multidimensional-array