Is the size allotted based on amount of dynamically allotted memory available? what happens when we reach this limit is there an exception thrown or it overwrites to some other
Just like std::cout
, if the stream fails (for whatever reason), the state of the buffer will be set (eofbit, failbit, or badbit). This will mean that operator bool()
for the stream will evaluate to false
.
std::ostringstream oss;
// a lot of output to oss here - causing a situation where you are out of available memory
if (!(oss << some_value))
{
// oss failed to write some_value!
}
Note: Prior to C++11, this was done via operator void*()
.
Also, If you wanted the stream to throw an exception (it won't by default), you can register it to throw using the std::ios::exceptions() function.