Following code doesn't throw exception and prints "success". Why ?
#include <iostream>
int main()
{
size_t size = size_t(1024)*1024*1024*1024*1024*1024*1024*1024;
char* data = new char[size];
if (data == NULL)
std::cout << "fail" << std::endl;
else
std::cout << "success" << std::endl;
return 0;
}
And if this is how it's meant to work, how do I check that I have enough memory ?
[Edit: made my stupid code a bit more correct, now it would at least fail on x64 if I remove two *1024
]
My compiler can answer this one:
$ g++ --version
g++ (GCC) 4.7.1 20120721 (prerelease)
Copyright (C) 2012 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -Wall -Wextra -pedantic q12507456.c++
q12507456.c++: In function 'int main()':
q12507456.c++:5:42: warning: integer overflow in expression [-Woverflow]
$
This is more than likely to be the fact that the number you are requesting is too large to be stored within an integer and you are experiencing overflow here and the memory allocated is in fact far far less than you think it is.
Here 2^80 = 1208925819614629174706176 according to http://en.wikipedia.org/wiki/Yobibyte
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With