Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I know the ACTUAL maximum number of elements a .net array of a given type can be allocated?
    text
    copied!<p>I know that all arrays in .net are limited to 2 GB, under this premise, I try not to allocate more that n = ((2^31) - 1) / 8 doubles in an array. Nevertheless, that number of elements still doesn't seem to be valid. Anyone knows how can I determine at run time the maximum number of elements given sizeof(T)?</p> <p>I know that whatever quantity approaching that number is just a lot of elements but, for all intents and purposes, let's say I need it.</p> <p>Note: I'm in a 64-bit environment, with a target platform for my application of AnyCPU, and at least 3100 MB free in RAM.</p> <p><strong>Update:</strong> Thank you all for your contributions and sorry I was so quiet. I apologise for the inconvenience. I have not been able to rephrase my question but I can add that, what I am looking for is solving something like this:</p> <pre><code>template &lt;class T&gt; array&lt;T&gt;^ allocateAnUsableArrayWithTheMostElementsPossible(){ return gcnew array&lt;T&gt;( ... ); } </code></pre> <p>The results in my own answer are <em>kinda</em> satisfactory but not good enough. Furthermore, I haven't test it on another machine (Kind of hard finding another machine with more than 4 GB). Besides, I have been doing some research on my own and it seems there's no cheap way to calculate this at run time. Anyhow, that was just a plus, none of the user of <em>what-I-am-trying-to-accomplish</em> can expect to use the feature I am trying to implement without having the capacity.</p> <p>So, in other words, I just want to understand why the maximum number of elements of an array don't add up to 2GB <em>ceteris paribus</em>. A top maximum is all I need for now.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload