Re: Allocations that overflow
Angel Tsankov <fn42551@fmi.uni-sofia.bg> wrote:
According to the standard, what must the following function do if it
is passed std::numeric_limits<std::size_t>::max()?
struct S
{
char a[64]; // Any size greater than 1 would do.
};
S* allocate(std::size_t size)
{
return new S[size];
}
It can do anything, or not be run at all. It goes like this:
5.3./1 The new-expression attempts to create an object...
Annex B
1 Because computers are finite, C++ implementations are inevitably
limited in the size of the programs they can successfully process...
2 The limits may constrain quantities that include those described below
or others...
- Size of an object
1.4/2 ... If a program contains no violations of the rules in this
International Standard, a conforming implementation shall, *within its
resource limits*, accept and correctly execute that program (emphasis
mine).
The implication is that if the program exceeds implementation resource
limits, it may be rejected or executed incorrectly.
But see:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#256
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#624
It seems likely that the next version of the standard would require
bad_alloc exception in this situation.
--
With best wishes,
Igor Tandetnik
With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925