掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
なぜヒープの解放でエラー? (ID:68292)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
ファイルサイズでバッファーを確保し そこに読み込むだけのプログラムなのですが 関数を抜けるとき(例ではwmain)に デバッグエラーになります。 buf( len + 1 )だとエラーにならないのですが +1する必要がないように思えて不思議です。 どなたかわかる方がいれば説明してもらえないでしょうか。 環境: ・Windows XP SP2 ・VC++ 2005 SP1 エラー内容: ------------------------------------------------- Debug Error! Program: e:\study\c++\heaptest\debug\HeapTest.exe HEAP CORRUPTION DETECTED: after Normal block (#74) at 0x004D0068. CRT detected that the application wrote to memory after end of heap buffer. (Press Retry to debug the application) ------------------------------------------------- サンプルコード: ------------------------------------------------- #include <fstream> #include <vector> int wmain( int argc, wchar_t* argv[] ) { if( argc < 2 ) return 1; std::wifstream ifs( argv[ 1 ], std::ios::binary ); if( !ifs ) { return 1; } ifs.seekg( 0, std::ios::end ); size_t len = ifs.tellg(); std::vector<unsigned char> buf( len ); ifs.seekg( 0, std::ios::beg ); for( size_t i = 0; i < buf.size(); i++ ) { ifs.read( reinterpret_cast<wchar_t*>( &buf[ i ] ), 1 ); } ifs.close(); return 0; } -------------------------------------------------
←解決時は質問者本人がここをチェックしてください。
更新する
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.