I wrote the following code snippet to allocate memory for an array:
try { int n = 0; cin >> n; double *temp = new double[n]; ... } catch(exception& e) { cout << "Standard exception: " << e.what() << endl; exit(1); }
Of course, I check n for negative values, etc., but when I enter some large number for 536 * (10 ^ 6), I don't get a bad-alloc exception, but "Invalid distribution size: 4294967295 bytes" Crash .
eg. I enter n = 536 * (10 ^ 6) → bad-alloc exception I enter n = 537 * (10 ^ 6) → Invalid distribution: 4294967295 Bytes → Failed
Any ideas why this is happening?
new double[n] operator new n * sizeof(double). operator new , , .
new double[n]
operator new
n * sizeof(double)
: n sizeof(double) , operator new , , , size_t. , , , , .
n
sizeof(double)
size_t
, , n <= SIZE_MAX / sizeof(double), .
n <= SIZE_MAX / sizeof(double)
Visual Studio , " " .
Properties → Linker → System → Enable Large Addresses, " (/LARGEADDRESSAWARE)"
In a 32-bit system, the address space of virtual memory cannot exceed 2 ^ 31-1 (4294967295) bytes.
You are trying to allocate 536000000*sizeof(double)bytes, which is obviously more.
536000000*sizeof(double)
Source: https://habr.com/ru/post/1539259/More articles:Passing multiple, single or no parameters via cakePHP URL - cakephpandroid app autoupdate if apk is present then remove - androidHow to set remember me always true or loggedin in grails? - spring-securityJava application to install APK on Android - javaIs it recommended to store program sources in a single file (as opposed to lib sources)? - goLimited C ++ Distribution Size - c ++@singleton ведет себя как @stateless bean - javaHow to dynamically change a Crystal Report database connection - c #Странное поведение шрифта после GraphicsEnvironment # getAvailableFontFamilyNames()? - javaHow to get qualified git url remote using url. .insteadOf? - gitAll Articles