std::bad_array_new_length(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’… 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:

  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. 🫢

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 new bad_array_new_length object (public member function)
  • operator= β€” replaces the bad_array_new_length object (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(). πŸ’…

  1. Default constructor.
  2. Copy constructor. If *this and other both have dynamic type std::bad_array_new_length then std::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 of std::exception)
  • what β€” returns an explanatory string [virtual] (virtual public member function of std::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 functions
  • operator 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