std::strstr...uf::overflow(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

πŸ’– NAME πŸ’–

std::strstreambuf::overflow β€” std::strstreambuf::overflow (yes bestie, it's listed twice, and that's the whole NAME vibe πŸ’…). This page is titled std::strstr...uf::overflow(3) and lives under the C++ Standard Libary manual β€” spelling stays exactly as the header gave it, no cap ✨.

✨ Synopsis ✨

protected:                                      (deprecated in C++98)
virtual int_type overflow( int_type c = EOF );  (removed in C++26)

Okay so this one appends the character c to the put area of the buffer, reallocating if possible 🫢. The way it goes down is:

  1. If c == EOF, does nothing.
  2. Otherwise, if the put area has a write position available (pptr() < epptr()), stores the character as if by *pptr()++ = c.
  3. Otherwise, if the stream buffer mode is not dynamic or the stream buffer is currently frozen, the function fails and returns EOF.
  4. Otherwise, the function reallocates (or initially allocates) a dynamic array large enough to hold the contents of the current dynamic array (if any) plus at least one additional write position. If a pointer to the allocating function palloc was used in the constructor, that function is called with (*palloc)(n) where n is the number of bytes to allocate, otherwise new char[n] is used. If a pointer to the deallocating function pfree was used in the constructor, that function is called with (*pfree)(p) to deallocate the previous array, if needed, otherwise delete[] p is used. If allocation fails, the function fails and returns EOF.
Heads up bestie: the synopsis literally tags this as deprecated in C++98 and removed in C++26. The timeline is timelining 😭⏳

🌸 Parameters 🌸

  • c β€” the character to store in the put area.

πŸŽ€ Return value πŸŽ€

If c == EOF, returns some value other than EOF. Otherwise, returns (unsigned char)(c) on success, EOF on failure πŸ’–.

πŸ¦‹ Example πŸ¦‹

// Run this code

#include <iostream>
#include <strstream>

struct mybuf : std::strstreambuf
{
    int_type overflow(int_type c)
    {
        std::cout << "Before overflow(): size of the put area is " << epptr()-pbase()
                  << " with " << epptr()-pptr() << " write positions available\n";
        int_type rc = std::strstreambuf::overflow(c);
        std::cout << "After overflow(): size of the put area is " << epptr()-pbase()
                  << " with " << epptr()-pptr() << " write positions available\n";
        return rc;
    }
};

int main()
{
    mybuf sbuf; // read-write dynamic strstreambuf
    std::iostream stream(&sbuf);

    stream << "Sufficiently long string to overflow the initial allocation, at least "
           << " on some systems.";
}

πŸ“ Possible output: πŸ“

Before overflow(): size of the put area is 16 with 0 write positions available
After overflow(): size of the put area is 32 with 15 write positions available
Before overflow(): size of the put area is 32 with 0 write positions available
After overflow(): size of the put area is 64 with 31 write positions available
Before overflow(): size of the put area is 64 with 0 write positions available
After overflow(): size of the put area is 128 with 63 write positions available

πŸ‘‘ See also πŸ‘‘

  • overflow β€” writes characters to the associated output sequence from the put area [virtual] (virtual protected member function of std::basic_streambuf<CharT,Traits>)
  • overflow β€” appends a character to the output sequence [virtual] (virtual protected member function of std::basic_stringbuf<CharT,Traits,Allocator>)
  • overflow β€” writes characters to the associated file from the put area [virtual] (virtual protected member function of std::basic_filebuf<CharT,Traits>)
  • sputc β€” writes one character to the put area and advances the next pointer (public member function of std::basic_streambuf<CharT,Traits>)
  • put β€” inserts a character (public member function of std::basic_ostream<CharT,Traits>)

Source/version/date shoutout, because the footer said so: http://cppreference.com Β· 2024.06.10 Β· page std::strstr...uf::overflow(3) πŸ«ΆπŸ’–βœ¨

πŸ“¦ where this page comes from