Questions

M

mem

If exception disabled, how to handle the situations of "out-of-memory" when
creating objects with "new"?

What is "Boost" about which people have been talking often? What platforms
does it support? Thanks!
 
J

Jacques Labuschagne

mem said:
If exception disabled, how to handle the situations of "out-of-memory" when
creating objects with "new"?

There are two forms of new. The regular one throws an exception when it
can't perform an allocation, the other one returns NULL. To use the
alternate one, say

char* s = new(nothrow) char[256];
What is "Boost" about which people have been talking often? What platforms
does it support? Thanks!

http://www.boost.org


Jacques.
 
R

Russell Hanneken

mem said:
If exception disabled, how to handle the situations of "out-of-memory"
when creating objects with "new"?

You mean, if you do something like this?

#include <new>

// . . .
int *p( new(std::nothrow) int[100000] );
// . . .

You would check to see if new succeeded by checking the value of p. If it's
non-zero, new succeeded. If it's zero, new failed. How you handle
situations of memory allocation failure depends on the context (just as when
you use exceptions).
What is "Boost" about which people have been talking often?

From the Boost web site (http://www.boost.org/):

The Boost web site provides free peer-reviewed portable C++ source
libraries. The emphasis is on libraries which work well with the C++
Standard Library. . . . A further goal is to establish "existing
practice" and provide reference implementations so that Boost libraries
are suitable for eventual standardization.
What platforms does it support?

http://www.boost.org/status/compiler_status.html
 
R

Robbie Hatley

mem said:
If exception disabled, how to handle the situations of "out-of-memory" when
creating objects with "new"?

You could try catching exceptions of type "std::bad_alloc", but that might
not work because by the time you're out of memory, there's so little resources
left that your OS will probably NOT be able to even run your exception-
handling routine.

I few days ago I tried writing a handler for std::bad_alloc in a Windows
program, and tested it by attempting to load 5,000,000,000 instances of
a struct into a map, but the handler never ran. Not enough memory left!
The system crashed, instead.

I was amused at what Bjarne Stroustrup says in "The C++ Programming
Language" about memory exhaustion: he says that the best way to "handle"
that situation is to make sure it never occurs in the first place! Ie, buy more
RAM or use less memory. (And don't leak.)
What is "Boost" about which people have been talking often? What platforms
does it support? Thanks!

Dunno. Try Googling it.


--
Cheers,
Robbie Hatley
Tustin, CA, USA
email: lonewolfintj at pacbell dot net
(Include "[ciao]" in subject to bypass spam filters.)
web: home dot pacbell dot net slant earnur slant
 

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

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top