Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::strstreambuf::~strstreambuf

      From cppreference.com
      <cpp‎ |io‎ |strstreambuf
       
       
       
       
      virtual ~strstreambuf();
      (deprecated in C++98)
      (removed in C++26)

      Destroys astd::strstreambuf object. if the object is managing a dynamically-allocated buffer (the buffer state is "allocated") and if the object is not frozen, then deallocates the buffer using the deallocation function provided at construction ordelete[] if none was provided.

      [edit]Parameters

      (none)

      [edit]Notes

      This destructor is typically called by the destructor ofstd::strstream.

      Ifstr() was called on a dynamicstrstream andfreeze(false) was not called after that, this destructor leaks memory.

      [edit]Example

      Run this code
      #include <iostream>#include <strstream> void* my_alloc(size_t n){std::cout<<"my_alloc("<< n<<") called\n";return newchar[n];} void my_free(void* p){std::cout<<"my_free() called\n";    delete[](char*)p;} int main(){{std::strstreambuf buf(my_alloc, my_free);std::ostream s(&buf);        s<<1.23<<std::ends;std::cout<< buf.str()<<'\n';        buf.freeze(false);}// destructor called here, buffer deallocated {std::strstreambuf buf(my_alloc, my_free);std::ostream s(&buf);        s<<1.23<<std::ends;std::cout<< buf.str()<<'\n';//      buf.freeze(false);}// destructor called here, memory leak!}

      Output:

      my_alloc(4096) called1.23my_free() calledmy_alloc(4096) called1.23
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/io/strstreambuf/%7Estrstreambuf&oldid=170645"

      [8]ページ先頭

      ©2009-2026 Movatter.jp