I get bad_alloc exception at i=33401936, consider unsigned long takes 4 bytes, it is running out of memory at approx 133mb. However 32 bit address space should support up to 2GB. I have a 4GB ram so it should suffice and task manager says 2.5/4 GB is free.
I am using VS2008 on windows 7. Can anybody comment why is it failing at 133mb only.
I am running following code, trying to see how much max memory can i allocate
This does not test what you believe you're testing.
The only thing that the C++ "new" does is guarantee you have a pointer to the type, and therefore can be accessed safely. Nothing more, nothing less. The compiler could have gotten that memory from a memory pool, a big array allocated from the stack, or yes, from the OS. But you really know nothing about how much real memory was really allocated, or if any real memory was allocated at all.
For example, the compiler has the option of allocating more memory than you ask for. For example, extra bytes for guard bytes, for debugging purposes, or for the compiler's own internal heap memory management. So in other words, when you say "new T", at the end of the allocation, the total memory used for this request will more than likely be greater than sizeof(T).
If you really want to get a more realistic test, you have to use the OS specific heap allocation functions (for Windows you have HeapAlloc, etc.), and not "new", malloc(), or any such C++ heap-store/allocation functions.
Last edited by Paul McKenzie; June 11th, 2009 at 09:21 AM.