[Bug c/65350] New: [C++14] operator new[] should not be called if # of initializer elements exceeds # of elements
kariya_mitsuru at hotmail dot com
gcc-bugzilla@gcc.gnu.org
Sun Mar 8 16:49:00 GMT 2015
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65350
Bug ID: 65350
Summary: [C++14] operator new[] should not be called if # of
initializer elements exceeds # of elements
Product: gcc
Version: 5.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: kariya_mitsuru at hotmail dot com
Please see the sample code below.
========================== sample code ==========================
#include <iostream>
#include <new>
void* operator new[](std::size_t size)
{
std::cout << "my operator new[](" << size << ")" << std::endl;
return ::operator new(size);
}
int main()
{
int i = 1;
try {
int* p = new int[i]{ 1, 2 };
delete[] p;
} catch (const std::bad_array_new_length& e) {
std::cout << e.what() << std::endl;
}
}
========================== sample code ==========================
========================== output ==========================
my operator new[](4)
std::bad_array_new_length
========================== output ==========================
cf. http://melpon.org/wandbox/permlink/tQFp5fpPXT5mZu34
The C++14 standard 5.3.4[expr.new]/p.7 says,
The expression in a noptr-new-declarator is erroneous if:
...
--- the new-initializer is a braced-init-list and the number of array
elements
for which initializers are provided (including the terminating '\0' in
a
string literal (2.13.5)) exceeds the number of elements to initialize.
... Otherwise, a new-expression with an erroneous expression **does not call
an allocation function** and terminates by throwing an exception of a type
that would match a handler (15.3) of type std::bad_array_new_length
(18.6.2.2).
...
(emphasis mine)
So, I think that the sample code above should output only
========================== output ==========================
std::bad_array_new_length
========================== output ==========================
More information about the Gcc-bugs
mailing list