1

私の人生では、なぜ nothrow が機能しないのかわかりません。さまざまなヘッダーを試しました。また、 new ヘッダーと memory ヘッダーを交換しても、同じ出力が得られます。

0x61ff50    6422400
How many numbers would you like to type: 1000000000

terminate called after throwing an instance of 'std::bad_array_new_length'
  what():  std::bad_array_new_length

私が間違っていることについてのアイデアはありますか?プログラムがクラッシュせずに配列サイズに任意の値を使用することなく、nothrow を与える方法を探しています。最大サイズ内で配列を使用する必要があるようです。これにより、プログラムが安定するため、nothrow オブジェクトをテストできません。例外を使用する唯一のオプションを残してください。

#include <iostream>
#include <memory> 
#include <new>    // also either/or this 


using namespace std ;

int main ()
{
    int i, n ;

    int *p ;

    cout << p << "    " << *p << endl ;


    cout << "How many numbers would you like to type: " ;

    cin >> i ;


    p = new (nothrow) int[i] ;

    if (!p)
    {
        cout << "Error: memory could not be allocated" ;
    }

    else
    {
        for (n = 0 ; n < i ; n ++)
        {
            cout << "Enter number: " ;

            cin >> p[n] ;
        }
        cout << endl << p << endl << *p << endl ;
        cout << "You have entered: " ;

        for (n = 0 ; n < i ; n++ )
            cout << p[n] << ", " ;


        delete[] p ;

        cout << endl << p << "   " << *(p + 0) << endl ;

        p = nullptr ;

        cout << endl << p << endl ;

        p = &i ;

        cout << endl << p << "    " << *p ;

    }

    cin.get() ;

    return 0 ;
}
4

0 に答える 0