Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/new/bad array new length"

From cppreference.com
< cpp‎ | memory‎ | new
(+inheritance diagram)
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
Line 59: Line 59:
 
{{dcl list template | cpp/memory/new/dcl list bad_alloc}}
 
{{dcl list template | cpp/memory/new/dcl list bad_alloc}}
 
{{dcl list end}}
 
{{dcl list end}}
 +
 +
[[de:cpp/memory/new/bad array new length]]
 +
[[es:cpp/memory/new/bad array new length]]
 +
[[fr:cpp/memory/new/bad array new length]]
 +
[[it:cpp/memory/new/bad array new length]]
 +
[[ja:cpp/memory/new/bad array new length]]
 +
[[pt:cpp/memory/new/bad array new length]]
 +
[[ru:cpp/memory/new/bad array new length]]
 +
[[zh:cpp/memory/new/bad array new length]]

Revision as of 16:01, 2 November 2012

Template:cpp/memory/new/bad array new length/navbar Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <new>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td >
class bad_array_new_length;
</td>

<td class="t-dcl-nopad"> </td> <td > (since C++11) </td> </tr> Template:ddcl list end

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/exceptioncpp/memory/new/bad allocstd-bad array new length-inheritance.svg

Inheritance diagram

Contents

Member functions

constructs the bad_array_new_length object
(public member function)

Inherited from std::bad_alloc

Inherited from std::exception

Member functions

[virtual]
destroys the exception object
(virtual public member function of std::exception) [edit]
[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.

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';
    }
}

See also

Template:cpp/memory/new/dcl list operator newTemplate:cpp/memory/new/dcl list bad alloc