Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy can't I reserve 1,000,000,000 in my vector?
    text
    copied!<p>When I type in the foll. code, I get the output as 1073741823.</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; int main() { vector &lt;int&gt; v; cout&lt;&lt;v.max_size(); return 0; } </code></pre> <p>However when I try to resize the vector to 1,000,000,000, by <code>v.resize(1000000000);</code> the program stops executing. How can I enable the program to allocate the required memory, when it seems that it should be able to?</p> <p>I am using MinGW in Windows 7. I have 2 GB RAM. Should it not be possible? In case it is not possible, can't I declare it as an array of integers and get away? BUt even that doesn't work.</p> <p>Another thing is that, suppose I would use a file(which can easily handle so much data ). How can I let it read and write and the same time. Using <code>fstream file("file.txt', ios::out | ios::in );</code> doesn't create a file, in the first place. But supposing the file exists, I am unable to use to do reading and writing simultaneously. WHat I mean is this : Let the contents of the file be <code>111111</code> Then if I run : -</p> <pre><code>#include &lt;fstream&gt; #include &lt;iostream&gt; using namespace std; int main() { fstream file("file.txt",ios:in|ios::out); char x; while( file&gt;&gt;x) { file&lt;&lt;'0'; } return 0; } </code></pre> <p>Shouldn't the file's contents now be <code>101010</code> ? Read one character and then overwrite the next one with 0 ? Or incase the entire contents were read at once into some buffer, should there not be atleast one 0 in the file ? <code>1111110</code> ? But the contents remain unaltered. Please explain. Thank you.</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