I tried to test bad_alloc
exception by passing some negative arguments to new[]
. When passing small negative numbers I get what I hoped for - a bad_alloc
. However, when passing -1
, I can see that my object is constructed thousands of times (I print static counter in constructor) and the application terminates with segfault.
new[]
converts signed integer to size_t
, so -1
is the max of size_t
and -2
is the maximum - 1
and so on.
So why new[]
throws exception when receiving some huge number, but tries to allocate when receiving the max of size_t
? What is the difference between 1111...1
and 1111...0
for new[]
? :)
Thanks in advance!