std::strstreambuf::freeze
From cppreference.com
< cpp | io | strstreambuf
| void freeze( bool freezefl = true ); |
(C++98で非推奨) (C++26で削除) |
|
バッファが動的確保を使用している場合、ストリームの凍結状態を freezefl に設定します。
ストリームが凍結されている間、overflow() はバッファを再割り当てせず、destructor はバッファを解放しません(これによりメモリリークが発生します)。
目次 |
[編集] パラメータ
| freezefl | - | 凍結状態に設定する新しい値 |
[編集] 戻り値
(なし)
[編集] 注釈
str() への各呼び出しは、返されるポインタの有効性を維持するためにストリームを凍結します。デストラクタがバッファを解放できるようにするには、明示的に freeze(false) を呼び出す必要があります。
[編集] 例
この例では、基になる配列の初期割り当ては16バイトでした。
このコードを実行
#include <iostream> #include <strstream> int main() { { std::strstream dyn; // dynamically-allocated read/write buffer dyn << "Test: " << 1.23; // note: no std::ends to demonstrate append behavior std::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 behavior std::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 の public メンバ関数) | |
| 自動再割り当てを無効/有効にします。 ( std::ostrstream の public メンバ関数) | |
| [virtual] |
strstreambuf オブジェクトを破棄します。オプションで文字配列を解放します。(virtual public メンバ関数) |
| [virtual] |
出力シーケンスに文字を追加します。動的で凍結されていない場合は、バッファを再割り当てまたは最初に割り当てることができます。 (virtual protected メンバ関数) |