开发者

how to find the maximum size of ostream class in c++

I am assigning std::ostream *pout_ = output_.get();

where output_ is scoped_ptr<std::stringstream> output_;

after assigning i am filling *pout_ <<"large strings"; appx 1172528 characters.

But after some extent i am not able to insert charcters inside *pout_. I tried surfing on the net whats the maximum size of this class but couldn't find.

Someone pl开发者_开发百科ease tell how much characters maximum i can store in *pout_. Is there any function which can tell me the maximum size of this class??


There are several possible causes for output to an ostream to fail. The most obvious is that the underlying supporting media (memory, in the case of an std::ostringstream) is full. Another is that you've reached some internal limit: a lot of systems have (or had) file size limits which would hit you long before the disk was full, and some systems have had similar constraints on single objects in memory (and the std::stringbuf class typically keeps its data in a single object). (There's also the possiblity of a hardware error, but if this occurs with std::stringbuf, i.e. a memory error, the hardware probably won't detect it.)

All of these mean that there is no hard limit as to how much you can write to a stream, string or otherwise. It all depends, and one time, you might succeed writing 2 GB, and the next fail after 1 MB or less. Practically speaking, in most cases, you should be aware of the fact that writes can fail, test the results (after a final flush) and be prepared to do something reasonable if they do.

In the specific case of string streams, of course, about the only failure you're likely to be able to detect is out of memory. (An ostream does not propagate an exception from the streambuf; it sets the badbit when one occurs, and if exceptions have been activated for badbit, it will throw its own exception, not rethrow the original one. Which means that an std::bad_alloc will not propagate out.)
A lot of applications don't handle out of memory, and should logically have set the new handler to abort. If you've set the new handler to abort, then you can probably forego such error checking on string outputs.


The answer depends on your compiler, platform etc. It also depends on the manner in which you write stuff to the stringstream (since it may need to reallocate memory for its buffer, potentially causing memory fragmentation and therefore under-utilisation).

The only practical way to get an idea of the limit is by conducting realistic experiments in your target environment. Even then, you should only use the results of such experiments as a rough guide.


A stringstream stores the data in a memory buffer. It will grow the buffer as needed, as long as it can.

You can continue to write to it until you run out of memory. There is not fixed limit.


It depends on a huge number of factors, such as

  • what load addresses are chosen for the shared objects you used
  • how much memory the rest of your program uses
  • how badly fragmented your address space has become

That's why you couldn't find any documented limit on the web.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜