首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::strstreambuf::freeze

void freeze( bool freezefl = true );

?

?

如果缓冲区使用动态分配,则将流的冻结状态设置为freezefl...

当溪流被冻结时,overflow()将不会重新分配缓冲区和destructor不会释放缓冲区%28,从而导致内存泄漏%29。

参数

freezefl

-

new value to set the freeze status to

返回值

%280%29

注记

每一次呼叫str()冻结流以保留它返回的指针的有效性。若要允许析构函数释放缓冲区,freeze(false)需要显式调用。

在本例中,基础数组的初始分配为16个字节。

二次

代码语言:javascript
复制
#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 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,
}

二次

产出:

二次

代码语言:javascript
复制
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'

二次

另见

freeze

disables/enables automatic reallocation (public member function of std::strstream)

freeze

disables/enables automatic reallocation (public member function of std::ostrstream)

(destructor) virtual

destructs a strstreambuf object, 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)

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com