std::strstreambuf::freeze(3)
π std::strstreambuf::freeze(3) π
C++ Standard Libary, bestie β this is the std::strstreambuf::freeze page, and itβs giving tiny toggle, big memory consequences. π
πΈ NAME πΈ
std::strstreambuf::freeze β okay so this is the freeze-status setter for a std::strstreambuf, and she is iconic but also a little retired.
β¨ Synopsis β¨
void freeze( bool freezefl = true ); (deprecated in C++98)
(removed in C++26)
Real talk: if the buffer uses dynamic allocation, this sets the frozen status of the stream to freezefl. π
While the stream is frozen, overflow() will not reallocate the buffer, and the destructor will not deallocate the buffer β which means a memory leak. π Not a vibe.
π Parameters π
freezeflβ the new value to set the freeze status to.
π Return value π
(none) β no return value, periodt. π«Ά
π¦ Notes π¦
Every call to str() freezes the stream to preserve the validity of the pointer it returns. β¨ So if you want the destructor to deallocate the buffer, freeze(false) needs to be called explicitly. No cap.
π Example π
In this example, initial allocation of the underlying array was for 16 bytes. Run this code, bestie:
#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,
}
π Output π
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'
π₯ See also π₯
freezeβ disables/enables automatic reallocation (public member function ofstd::strstream)freezeβ disables/enables automatic reallocation (public member function ofstd::ostrstream)destructor[virtual]β destructs astrstreambufobject, optionally deallocating the character array (virtual public member function)overflow[virtual]β appends a character to the output sequence, may reallocate or initially allocate the buffer if dynamic and not frozen (virtual protected member function)
signed off with love from http://cppreference.com, version/date 2024.06.10, page std::strstreambuf::freeze(3) π«Άβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms