Possible Duplicate:
maximum memory which malloc can allocate!
How can I know the maximum size of the heap I can occupy by malloc()
.
I use MS Visual Studio 2010.
Possible Duplicate:
maximum memory which malloc can allocate!
How can I know the maximum size of the heap I can occupy by malloc()
.
I use MS Visual Studio 2010.
There are operating system-dependent ways of finding out how much virtual memory is available for your process, but I do not know how to do this on windows. You can, however, find it out by doing a hunt+halving search, caling malloc with ever larger arguments until it fails, and then homing in on the value it balks at. Something like
for(i=1; v=malloc(i); i<<=1) free(v);
By this point you know that i/2 bytes is ok, while i bytes is not ok. Now do a bisection search for the actual maximum:
for(a=(i>>1), b=i; a < b-1;)
{
c=(a+b)>>1;
if(v=malloc(c)) { a=c; free(v); }
else b=c;
}
At this point, a
is the largest amount you can successfully allocate.
Well, why not just look at its argument... size_t
. So, you can allocate anything in the range of size_t
(SIZE_MAX
works as well, but it is based on size_t
anyway and, event if it weren't, malloc
s signature is still what you have to deal with).
Your question is a bit unclear, but if you're actually asking what the maximum heap size is then, theoretically, it is determined by the native pointer size. For example, on a 32-bit system a pointer can hold any address between 0 and 2^32-1 (of course, address 0 is not commonly valid).
Of course, in practice, there are details which must be considered, most of them OS specific. How much physical memory (RAM) is installed in the machine? If you're OS uses virtual addresses then this complicates things as you are no longer limited by physical RAM (though performance will drop of a cliff with incessant paging).
It helps to understand how virtual address space works when thinking about how much your program can allocate on a given platform.