c++ - 写入未打开的流有什么后果吗?

标签 c++ stl

如果我有这个:

std::ofstream empty;

for(int i = 0; i < 99999; ++i)
{
   empty << "Nothing..." << std::endl;
}

这是否会导致内存不足异常或任何其他问题,因为流正在将数据插入但它不会去任何地方?

谢谢

最佳答案

当文件流被默认构造时,指向内部缓冲区的6个指针被初始化为nullptr。由于没有可用内存,在流上尝试的任何 I/O 都会失败,并且 ios_base::badbitios_base::failbit 将设置为流状态。

Will this ever cause an out of memory exception or any other problems since the stream is getting data pushed in but it is not going anywhere?

在这种情况下,允许流抛出 std::bad_alloc

关于c++ - 写入未打开的流有什么后果吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20693758/

相关文章:

c++ - std::streambuf::in_aval 总是为 std::ostream 返回 0

c++ - 为 STL 迭代器重载运算符->

c++ - 比较列表/集合元素

c++ - 使用后缀数组和 LCP(-LR) 实现字符串模式匹配

c++ - Typedef 泛化

c++ - 什么是PC物化?

C++:C 字符串集

c++ - 如何将 BOOST_FOREACH 与两个 std::maps 一起使用?

c++ - 为什么 std::allocator::deallocate 需要大小?

c++ - 一个从 list<T> 转换为 vector<T> 的衬垫