std::bad_array_new_length

From cppreference.com
< cpp‎ | memory‎ | new
 
 
Utilities library
Type support (basic types, RTTI, type traits)
Dynamic memory management
Error handling
Program utilities
Variadic functions
Date and time
Function objects
(C++11)
Relational operators
Optional and any
(C++17)
(C++17)
Pairs and tuples
(C++11)
(C++17)
Swap, forward and move
(C++14)
(C++11)
(C++11)
Type operations
(C++11)
(C++17)
 
 
 
std::bad_array_new_length
 
Defined in header <new>
class bad_array_new_length;
(since C++11)

std::bad_array_new_length is the type of the object thrown as exceptions by the new-expressions to report invalid array lengths if

1) array length is negative

2) total size of the new array would exceed implementation-defined maximum value

3) the number of initializer-clauses exceeds the number of elements to initialize

Only the first array dimension may generate this exception; dimensions other than the first are constant expressions and are checked at compile time.

cpp/error/exception cpp/memory/new/bad allocstd-bad array new length-inheritance.svg
About this image

Inheritance diagram

Contents

[edit] Member functions

constructs the bad_array_new_length object
(public member function)

Inherited from std::bad_alloc

Inherited from std::exception

Member functions

[virtual]
destructs the exception object
(virtual public member function of std::exception)
[virtual]
returns an explanatory string
(virtual public member function of std::exception)

[edit] Notes

The override for the virtual member function what() may by provided, but is not required.

[edit] Example

Three conditions where std::bad_array_new_length should be thrown:

#include <iostream>
#include <new>
#include <climits>
 
int main()
{
    int negative = -1;
    int small = 1;
    int large = INT_MAX;
    try {
        new int[negative];           // negative size
        new int[small]{1,2,3};       // too many initializers
        new int[large][1000000];     // too large
    } catch(const std::bad_array_new_length &e) {
        std::cout << e.what() << '\n';
    }
}


[edit] See also

allocation functions
(function)
exception thrown when memory allocation fails
(class)