|
From: Nicolai S. <nic...@gm...> - 2013-06-21 20:28:20
|
Ah and I forgot to explain why there's a std::bad_alloc:
Christopher Subich <cs...@uw...> writes:
> The error you're receiving is thrown by the "new" operator, when the
> memory blob is being created to hold the data, so you would expect to
> see a similar error by calling new_arr = new double[4000000000L].
Basically, the problem is, that 4000000000L overflows an int, which, when
cast to a size_t, is 0xffffffffee6b2800:
Try this:
--8<---------------cut here---------------start------------->8---
#include <cstddef>
#include <iostream>
int
main(int argc, char *argv[])
{
const int int_size = static_cast<int>(4000000000);
const size_t size_t_size = static_cast<size_t>(int_size);
std::cout << "int_size == " << std::hex << int_size
<< " size_t_size == " << size_t_size << std::endl;
return 0;
}
--8<---------------cut here---------------end--------------->8---
Now, new[] alway takes a size_t and I'm pretty sure that your machine
doesn't have got a memory size of `0xffffffffee6b2800 * sizeof(double)`
bytes.
|