STL and method failure

M

Mark

Hi,

I can't seem to find any documentation as to how
STL indicates method failure, whether by return
code or exception.

e.g how would I test for out of memory case
when inserting into a vector

Mark
 
V

Victor Bazarov

Mark said:
I can't seem to find any documentation as to how
STL indicates method failure, whether by return
code or exception.

e.g how would I test for out of memory case
when inserting into a vector

You should catch std::bad_alloc exception.

V
 
T

tom_usenet

Hi,

I can't seem to find any documentation as to how
STL indicates method failure, whether by return
code or exception.

If you pass in the wrong parameters, you generally get undefined
behaviour, not any exception. Few STL methods can actually fail of
their own accord for any reason other than memory exhaustion.
e.g how would I test for out of memory case
when inserting into a vector

All expected exceptions are generally documented with the functions
that throw them (e.g. vector::at). However, many functions can fail if
memory is exhausted. The out of memory case depends on the allocator
argument of the container. With std::allocator, you'll get
std::bad_alloc on failure. Mostly exceptions will come from your own
classes that the STL is using as template parameters - e.g. if your
copy constructor can throw, that will be propogated by the STL
functions.

You'll probably want to read the appendix on library exception safety
in recent editions of Stroustrup's book:
http://www.research.att.com/~bs/3rd_safe0.html
He's kindly posted the PDF online.

Tom
 
A

Andrew Koenig

e.g how would I test for out of memory case
when inserting into a vector

Most operating systems these days do not offer a useful way of detecting
memory exhaustion. The reason is that they mostly use virtual memory, which
means that long before you run out of memory, you start doing huge amounts
of paging and the whole machine starts running incredibly slowly. Then the
user comes along and cancels the program.
 
A

Alexander Terekhov

Andrew said:
Most operating systems these days do not offer a useful way of detecting
memory exhaustion. The reason is that they mostly use virtual memory, which
means that long before you run out of memory, ...

setrlimit(RLIMIT_AS, ...);

regards,
alexander.
 
P

Prateek R Karandikar

e.g how would I test for out of memory case
setrlimit(RLIMIT_AS, ...);

What do you mean? There is no such thing as "setrlimit", "RLIMIT_AS"
in Standard C++.

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
To iterate is human, to recurse divine.
-L. Peter Deutsch
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top