如果我有这个:
std::ofstream empty;
for(int i = 0; i < 99999; ++i)
{
empty << "Nothing..." << std::endl;
}
这是否会导致内存不足异常或任何其他问题,因为流正在将数据插入但它不会去任何地方?
谢谢
最佳答案
当文件流被默认构造时,指向内部缓冲区的6个指针被初始化为nullptr
。由于没有可用内存,在流上尝试的任何 I/O 都会失败,并且 ios_base::badbit
和 ios_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/