0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

HackerRankのExceptional Server問題について(C++)

0
Posted at

Exceptional Server問題

を解いていたら、ハマったので備忘録として書きます。

cerrでエラーを出力したら怒られた

標準出力の結果のみを確認しているみたい。
実際のコードは下記の通り。(ネタバレ注意)
cerrcoutにしたら正解になりました。
他のコンテストでも当てはまりそうだから、勉強になりました。

try
        {
            auto temp = Server::compute(A, B);
            std::cout << temp << endl;
        }
        catch (const std::bad_alloc &e)
        {
            std::cerr << "Not enough memory" << '\n';
        }
        catch (const std::exception &e)
        {
            std::cerr << "Exception: " << e.what() << '\n';
        }
        catch (...)
        {
            std::cerr << "Other Exception" << endl;
        }

mac環境だと、bad_alloc例外が出ずkillされる。

2
-8 5
1435434255433 5

問題のサンプル入力のような物を私の環境で入力すると、bad_allocを返しませんでした。
オーバーコミットという仕組みが働いているらしく、bad_allocではなくkillされてしまう、、、。
(私の環境は、visual stdio code 1.46.0,Mac OS 10.15.4,clang:Apple clang version 11.0.3 (clang-1103.0.32.29)です)
windowsだとbad_allocを返してくれるらしい。
参照:大きなサイズの配列を確保する際にエラーがでないことと、macOS上でコードの挙動が変わること。

私の調べた限りだと、bad_allcを出すためにはオーバコミット機能を止めるしかないみたいです。(自分では確かめてない)
c++ bad_alloc exception not catched

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?