Reputation: 54133
If I have this:
std::ofstream empty;
for(int i = 0; i < 99999; ++i)
{
empty << "Nothing..." << std::endl;
}
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?
Thanks
Upvotes: 0
Views: 170
Reputation: 96835
When the file stream is default-constructed, the 6 pointers to the internal buffer are initialized to nullptr
. Any I/O attempted on the stream will fail because there is no available memory, and ios_base::badbit
and ios_base::failbit
will be set in the stream state.
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?
The stream is allowed to throw std::bad_alloc
in this case.
Upvotes: 3
Reputation: 477358
You got it wrong. No data is "getting pushed" anywhere. If the stream is not opened, all output operations on it will fail, as you can easily tell yourself:
assert(!(empty << "foo"));
Upvotes: 1
Reputation: 8839
If the stream is not open, it should be treated as NULL
. So, the data being sent to the stream should be lost.
Upvotes: 0