Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | memory‎ | new
m (r2.7.3) (Robot: Adding de, es, fr, it, ja, pt, ru, zh)
m (Shorten template names. Use {{lc}} where appropriate.)
Line 1: Line 1:
 
{{cpp/title|bad_array_new_length}}
 
{{cpp/title|bad_array_new_length}}
 
{{cpp/memory/new/bad_array_new_length/navbar}}
 
{{cpp/memory/new/bad_array_new_length/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header | new}}
+
{{dcl header | new}}
{{ddcl list item | notes={{mark since c++11}} | 1=
+
{{dcl | notes={{mark since c++11}} | 1=
 
class bad_array_new_length;
 
class bad_array_new_length;
 
}}
 
}}
{{ddcl list end}}
+
{{dcl end}}
  
 
{{tt|std::bad_array_new_length}} is the type of the object thrown as exceptions by the [[cpp/language/new|new-expressions]] to report invalid array lengths if
 
{{tt|std::bad_array_new_length}} is the type of the object thrown as exceptions by the [[cpp/language/new|new-expressions]] to report invalid array lengths if
Line 21: Line 21:
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list mem ctor | cpp/memory/new/bad_array_new_length/bad_array_new_length | constructs the {{tt|bad_array_new_length}} object}}
+
{{dsc mem ctor | cpp/memory/new/bad_array_new_length/bad_array_new_length | constructs the {{tt|bad_array_new_length}} object}}
{{dcl list end}}
+
{{dsc end}}
  
 
{{cpp/memory/new/bad_alloc/inherit}}
 
{{cpp/memory/new/bad_alloc/inherit}}
Line 33: Line 33:
 
===Example===
 
===Example===
 
{{example
 
{{example
  | Three conditions where {{c|std::bad_array_new_length}} should be thrown:
+
  | Three conditions where {{lc|std::bad_array_new_length}} should be thrown:
 
  | code=
 
  | code=
 
#include <iostream>
 
#include <iostream>
Line 55: Line 55:
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/memory/new/dcl list operator_new}}
+
{{dsc inc | cpp/memory/new/dcl list operator_new}}
{{dcl list template | cpp/memory/new/dcl list bad_alloc}}
+
{{dsc inc | cpp/memory/new/dcl list bad_alloc}}
{{dcl list end}}
+
{{dsc end}}
  
 
[[de:cpp/memory/new/bad array new length]]
 
[[de:cpp/memory/new/bad array new length]]

Revision as of 19:14, 31 May 2013

Template:cpp/memory/new/bad array new length/navbar

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/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