ロゴ メインコンテンツへ
RSSフィード
「ソフトウェア開発」に関連する記事一覧

C++ inform memory allocating error with using set_new_handler

2010/11/24
(この記事の文字数: 465)
C++ 
set_new_handlerを使ってnew演算子でメモリ確保に失敗した場合にエラーメッセージを表示するプログラム例です。

mainのnewでメモリ確保に失敗するとinformMemoryLeak関数が呼ばれます。

This is a code for informing failure in allocating memory with using set_new_hander.
The new operator in main() called informMemoryLeak function if memory allocation has failed.

Example


#include <iostream>

using namespace std;

void informMemoryLeak(){
    cerr<<"There is not enough memory.\n";
    abort();
}

int main(){
    set_new_handler(informMemoryLeak);
    char *bigString = new char[0x7fffffff];
    return 0;
}



  このエントリーをはてなブックマークに追加  

<<「ソフトウェア開発」の記事一覧に戻る

「ソフトウェア開発」の前の記事 >>

コメント(0 件)



コンテンツロード: 0.0081 sec
Copyright(C)2006-2024 puarts All Rights Reserved