# std::strstreambuf::freeze

> C++ Atlas: `cppatlas.com/w/cpp/io/strstreambuf/freeze` | Source: https://en.cppreference.com/w/cpp/io/strstreambuf/freeze | Domain: Input/output | Standards: C++26

If the buffer uses dynamic allocation, sets the frozen status of the stream to freezefl.

## Official Specification Summary (cppreference)

```cpp
void freeze( bool freezefl = true ); // (deprecated in C++98) (removed in C++26)
```

If the buffer uses dynamic allocation, 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 (thereby causing a memory leak).

### Parameters

- `freezefl` - new value to set the freeze status to

### Return value

(none)

### Notes

Every call to `str()` freezes the stream to preserve the validity of the pointer it returns. To allow the destructor to deallocate the buffer, `freeze(false)` needs to be called explicitly.

### Example

In this example, initial allocation of the underlying array was for 16 bytes.

```cpp
#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:

```text
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`](https://en.cppreference.com/w/cpp/io/strstream/freeze): disables/enables automatic reallocation (public member function of `std::strstream`)
- [`freeze`](https://en.cppreference.com/w/cpp/io/ostrstream/freeze): disables/enables automatic reallocation (public member function of `std::ostrstream`)
- [(destructor)](https://en.cppreference.com/w/cpp/io/strstreambuf/~strstreambuf) [virtual]: destructs a `strstreambuf` object, optionally deallocating the character array (virtual public member function)
- [`overflow`](https://en.cppreference.com/w/cpp/io/strstreambuf/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)

---
Attribution: content from https://en.cppreference.com/w/cpp/io/strstreambuf/freeze (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.