std::strstreambuf::freeze
提供: cppreference.com
< cpp | io | strstreambuf
void freeze(bool freezefl =true); | ||
バッファが動的確保を使用する場合、ストリームの凍結状態を freezefl
に設定します。
ストリームが凍結されている間、 overflow() はバッファを再確保せず、 destructor はバッファを解放しません (そのためメモリリークを発生させます)。
目次 |
[編集]引数
freezefl | - | 設定する凍結状態の新しい値 |
[編集]戻り値
(なし)
[編集]ノート
str() のすべての呼び出しは返されたポインタの有効性を維持するためにストリームを凍結させます。 デストラクタがバッファを解放できるようにするためには、 freeze(false) が明示的に呼ばれる必要があります。
[編集]例
この例では、ベースとなる配列の初期確保は16バイトです。
Run this code
#include <strstream>#include <iostream> int main(){{std::strstream dyn;// dynamically-allocated read/write buffer dyn <<"Test: "<<1.23;// note: no std::ends to demonstrate append behaviorstd::cout<<"dynamic buffer holds "<< dyn.pcount()<<" characters: '";std::cout.write(dyn.str(), dyn.pcount())<<"'\n";// the buffer is now frozen, further output will not make the buffer grow dyn <<"more output, hopefully enough to run out of the allocated space"<<std::ends;std::cout<<"After more output, it holds "<< dyn.pcount()<<" characters: '"<< dyn.str()<<"'\n"; dyn.freeze(false);// unfreeze before destructor}// memory freed by the destructor {char arr[20];std::ostrstream st(arr, sizeof arr);// fixed-size buffer st <<1.23;// note: no std::ends to demonstrate append behaviorstd::cout<<"static buffer holds "<< st.pcount()<<" characters: '";std::cout.write(st.str(), st.pcount());std::cout<<"'\n"; st <<"more output, hopefully enough to run out of the allocated space"<<std::ends;std::cout<<"static buffer holds "<< st.pcount()<<" characters: '";std::cout.write(st.str(), st.pcount());std::cout<<"'\n";}// nothing to deallocate, no need to unfreeze,}
出力:
dynamic buffer holds 10 characters: 'Test: 1.23' After more output, it holds 16 characters: 'Test: 1.23more o' static buffer holds 4 characters: '1.23' static buffer holds 20 characters: '1.23more output, hop'
[編集]関連項目
自動再確保を無効化または有効化します ( std::strstream のパブリックメンバ関数) | |
自動再確保を無効化または有効化します ( std::ostrstream のパブリックメンバ関数) | |
[仮想] | strstreambuf オブジェクトを破棄し、オプションで文字配列を解放します (仮想パブリックメンバ関数) |
[仮想] | 出力シーケンスに文字を追加します。 動的な場合、または凍結されていない場合、バッファを再確保または初期確保することがあります (仮想プロテクテッドメンバ関数) |