Good day. When transferring a Linux application to the win32 (mingw32) platform, there was a problem of reserving memory when creating an array of the maximum allowable size. If the Linux code works without problems:
int V_V = std::numeric_limits<int>::max(); std::unique_ptr<short[]> space{new short[V_V]}; then an exception is generated in win32. I tried to programmatically find the maximum allowable value for this platform:
try { std::unique_ptr<short[]> space{new short[V_V]}; if (found = step_setup()) cout << V_V << ": array created on " << &space[0] << endl; } catch(std::exception & e) { show_err(e); V_V -= step; } and suddenly found out that the permissible limit values ​​for creating an array are not constant:
f:\cDev\test_limit.exe 2147483647: std::bad_array_new_length 1047483647: std::bad_alloc 829153681: array created on 0x1dc2020 f:\cDev\test_limit.exe 2147483647: std::bad_array_new_length 1047483647: std::bad_alloc 828911645: array created on 0x1e30020 And they can change in 7m (!) Level: from 828911645 to 829153681.
Is there a software API in the standard library (without "windows.h") to get the size limit value for the array being created?
Here is the complete test code:
#include <iostream> #include <memory> #include <limits> using std::cout; using std::endl; int V_V = std::numeric_limits<int>::max(); // creating array size int step = 100000000; // step for search bool step_setup() { if(step > 1) { V_V += step; step = step / 10; return false; } else { return true; } } // Display only first error message void show_err(std::exception & e) { static std::string err{}; if(err != e.what()) { err = e.what(); cout << V_V << ": " << err << endl; } return; } int main() { cout << endl; bool found = false; while (!found) { try { std::unique_ptr<short[]> space{new short[V_V]}; if (found = step_setup()) cout << V_V << ": array created on " << &space[0] << endl; } catch(std::exception & e) { show_err(e); V_V -= step; } } return EXIT_SUCCESS; }
MEMORYSTATUSEX statex; statex.dwLength = sizeof(statex); GlobalMemoryStatusEx(&statex);ullAvailPageFile- max array size, if not backup,ullAvailVirtual. Can I get the full code, do not understand something, it shows the address of the highlighted byte?) - J. Doe3 * .ullTotalPhys - .ullTotalPageFile + .ullAvailPageFile, you can reserve more, but I don’t understand why I don’t use it - J. Doe