What is the largest amount of memory I can allocate on my MacBook Pro?
<p>I'm trying to figure out how much memory I can allocate before the allocation will fail.</p>

<p>This simple C++ code allocates a buffer (of size 1024 bytes), assigns to the last five characters of the buffer, reports, and then deletes the buffer. It then doubles the size of the buffer and repeats until it fails.</p>

<p>Unless I'm missing something, the code is able to allocate up to 65 terabytes of memory before it fails on my MacBook Pro. Is this even possible? How can it allocate so much more memory than I have on the machine? I must be missing something simple.</p>

<pre><code>int main(int argc, char *argv[])
{
        long long size=1024;
        long cnt=0;
        while (true)
        {
                char *buffer = new char[size];
                // Assume the alloc succeeded. We are looking for the failure after all.

                // Try to write to the allocated memory, may fail
                buffer[size-5] = 'T';
                buffer[size-4] = 'e';
                buffer[size-3] = 's';
                buffer[size-2] = 't';
                buffer[size-1] = '\0';

                // report
                if (cnt&lt;10)
                        cout &lt;&lt; "size[" &lt;&lt; cnt &lt;&lt; "]: " &lt;&lt; (size/1024.) &lt;&lt; "Kb ";
                else if (cnt&lt;20)
                        cout &lt;&lt; "size[" &lt;&lt; cnt &lt;&lt; "]: " &lt;&lt; (size/1024./1024.) &lt;&lt; "Mb ";
                else
                        cout &lt;&lt; "size[" &lt;&lt; cnt &lt;&lt; "]: " &lt;&lt; (size/1024./1024./1024.) &lt;&lt; "Gi ";
                cout &lt;&lt; "addr: 0x" &lt;&lt; (long)buffer &lt;&lt; " ";
                cout &lt;&lt; "str: " &lt;&lt; &amp;buffer[size-5] &lt;&lt; "\n";

                // cleanup
                delete [] buffer;

                // double size and continue
                size *= 2;
                cnt++;
        }
        return 0;
}
</code></pre>