std::bad_array_new_length(3)
π NAME π
Okay bestie, you're on the manual page for std::bad_array_new_length β from the C++ Standard Libary manual, section 3 β and the original NAME line is literally std::bad_array_new_length - std::bad_array_new_length (the name, the whole name, and nothing but the name, we love a minimalist moment π). This page is all about the exception type for invalid array lengths, no cap β¨.
πΈ SYNOPSIS πΈ
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:
- Array length is negative,
- Total size of the new array would exceed implementation-defined maximum value,
- 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. π«Ά
There's also an inheritance diagram file called std-bad array new length-inheritance.svg, labeled Inheritance diagram β very demure, very visual π.
π MEMBER FUNCTIONS π
constructorβ constructs a newbad_array_new_lengthobject (public member function)operator=β replaces thebad_array_new_lengthobject (public member function)whatβ returns the explanatory string (public member function)
π std::bad_array_new_length::bad_array_new_length π
bad_array_new_length() noexcept;
(1) (since C++11)
bad_array_new_length( const bad_array_new_length& other )
(2) (since C++11)
noexcept;
Constructs a new bad_array_new_length object with an implementation-defined null-terminated byte string which is accessible through what(). π
- Default constructor.
- Copy constructor. If
*thisandotherboth have dynamic typestd::bad_array_new_lengththenstd::strcmp(what(), other.what()) == 0.
Parameters
otherβ another exception object to copy
π§ std::bad_array_new_length::operator= π§
bad_array_new_length& operator=( const bad_array_new_length& other )
(since C++11)
noexcept;
Assigns the contents with those of other. If *this and other both have dynamic type std::bad_array_new_length then std::strcmp(what(), other.what()) == 0 after assignment.
Parameters
otherβ another exception object to assign with
Return value
*this
π¦ std::bad_array_new_length::what π¦
virtual const char* what() const noexcept; (since C++11)
Returns the explanatory string.
Parameters
(none)
Return value
Pointer to a null-terminated string with explanatory information. The string is suitable for conversion and display as a std::wstring. The pointer is guaranteed to be valid at least until the exception object from which it is obtained is destroyed, or until a non-const member function (e.g. copy assignment operator) on the exception object is called. π₯
β NOTES β
Implementations are allowed but not required to override what(). Ngl, that's the whole note β don't start drama about it π.
π INHERITED FROM std::bad_alloc π
This page says Inherited from std::bad_alloc β that's the note, bestie. π«Ά
π INHERITED FROM std::exception π
πΈ MEMBER FUNCTIONS πΈ
destructorβ destroys the exception object[virtual](virtual public member function ofstd::exception)whatβ returns an explanatory string[virtual](virtual public member function ofstd::exception)
π₯ EXAMPLE π₯
Three conditions where std::bad_array_new_length should be thrown:
// Run this code
#include <climits>
#include <iostream>
#include <new>
int main()
{
try
{
int negative = -1;
new int[negative];
}
catch (const std::bad_array_new_length& e)
{
std::cout << "1) " << e.what() << ": negative size\n";
}
try
{
int small = 1;
new int[small]{1,2,3};
}
catch (const std::bad_array_new_length& e)
{
std::cout << "2) " << e.what() << ": too many initializers\n";
}
try
{
long large = LONG_MAX;
new int[large][1000];
}
catch (const std::bad_array_new_length& e)
{
std::cout << "3) " << e.what() << ": too large\n";
}
std::cout << "End\n";
}
Possible output:
1) std::bad_array_new_length: negative size
2) std::bad_array_new_length: too many initializers
3) std::bad_array_new_length: too large
End
π SEE ALSO π
operator newβ allocation functionsoperator new[]β (function)bad_allocβ exception thrown when memory allocation fails (class)
πΈ source/version/date sign-off, bestie: http://cppreference.com Β· 2024.06.10 Β· std::bad_array_new_length(3) β bye, go catch those invalid array lengths πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms