命名空间
变体
操作

std::strstreambuf::overflow

来自 cppreference.cn
< cpp‎ | io‎ | strstreambuf
protected:
virtual int_type overflow( int_type c = EOF );
(在 C++98 中已弃用)
(在 C++26 中已移除)

将字符 c 追加到缓冲区的放置区域,如果可能则重新分配。

1) 如果 c == EOF,则不执行任何操作。
2) 否则,如果放置区域有可用的写入位置 (pptr() < epptr()),则存储字符,如同 *pptr()++ = c
3) 否则,如果流缓冲区模式不是动态的,或者流缓冲区当前被冻结,则函数失败并返回 EOF
4) 否则,该函数重新分配(或初始分配)一个动态数组,该数组足够大以容纳当前动态数组(如果有)的内容,外加至少一个额外的写入位置。如果在构造函数中使用了指向分配函数 palloc 的指针,则调用该函数时使用 (*palloc)(n),其中 n 是要分配的字节数;否则使用 new char[n]。如果在构造函数中使用了指向释放函数 pfree 的指针,则调用该函数时使用 (*pfree)(p) 以释放之前的数组(如果需要);否则使用 delete[] p。如果分配失败,则函数失败并返回 EOF

内容

[编辑] 参数

c - 要存储在放置区域中的字符

[编辑] 返回值

如果 c == EOF,则返回 EOF 以外的某个值。否则,成功时返回 (unsigned char)(c),失败时返回 EOF

[编辑] 示例

#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.";
}

可能的输出

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

[编辑] 参见

[虚拟]
将字符从放置区域写入到关联的输出序列
(std::basic_streambuf<CharT,Traits> 的虚拟保护成员函数) [编辑]
[虚拟]
将一个字符追加到输出序列
(std::basic_stringbuf<CharT,Traits,Allocator> 的虚拟保护成员函数) [编辑]
[虚拟]
将字符从放置区域写入到关联的文件
(std::basic_filebuf<CharT,Traits> 的虚拟保护成员函数) [编辑]
将一个字符写入放置区域并移动下一个指针
(std::basic_streambuf<CharT,Traits> 的公共成员函数) [编辑]
插入一个字符
(std::basic_ostream<CharT,Traits> 的公共成员函数) [编辑]