throwing out of memory exception in c++ doesnt work

C

Chris \( Val \)

| Chris ( Val ) wrote:
| > | >> On Mon, 01 Mar 2004 03:45:44 GMT, Stephan
| >>
| >>> why does the following code not work????
| >>> after compiling and running it will just say killed after all my
| >>> memory filled up
| >>> any suggestions?
| >>>
| >>> #include <iostream>
| >>> using namespace std;
| >>>
| >>> void out_of_mem() {
| >>> cout << "problem\n";
| >>> }
| >>>
| >>> // void ?
| >>> int main() {
| >>>
| >>> set_new_handler(out_of_mem);
| >>>
| >>> while(1) {
| >>> int * tst = new int[1024];
| >>> if (!tst)
| >>> cout << "problem\n";
| >>> }
| >
| > [snip]
| >
| > C'mon Leor :).
| >
| > You know that 'new' does not return '0' unless you tell it to:
| >
| > int * tst = new ( std::nothrow )int[ 80000000 ];
|
| Unless you are, like Leor (and me) using Microsoft Visual C++ .Net 2003
| (7.1), in which case new does return NULL in certain situations. In fact,
| new will only throw exceptions in VC++ if your program links with the
| Standard C++ Library (libcp.lib) *before* it links with the Standard C
| Library (libc.lib). Alternatively, you can force the throwing new behaviour
| by linking with thrownew.obj.
|
http://msdn.microsoft.com/library/en-us/vclang/html/_pluslang_The_new_and_delete_Operators.asp

That's strange - My VS.NET 2002 does throw std::bad_alloc
as it should. Are they going backwards :).

Cheers.
Chris Val
 
S

Stephan

The combination of your saying you're allocating "a couple of small int
arrays" and memory is "filling up" leads me to suspect a memory leak; in
your original code you'd allocated memory using new but never deleted it,
and I figured you were just trying to deliberately exhaust memory to test
the exception handling behavior you were asking about. Perhaps I had that
wrong? Have you checked to make sure every chunk of memory you've obtained
via new is being handed to delete before the pointer get re-used?
-leor



Leor Zolman
BD Software
(e-mail address removed)
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
oh yea yea yea, sorry i shoulda mentioned that, this does have a memory
leak!!! i am just trying to keep allocating! sorry i am putting this topic
off so much, it sure seems interesting what people figure out.
yea i am not even thinking about deleting or keeping track of the memory i
allocated a reuse the pointer just to get more. maybe that is why i am not
allowed to handle it, but the kernel kills it.
i do not really run into memory issues, just seems good to know, maybe
small embedded project or whatnot. since i am starting java soon it also
seems helpful to make use of try and catch blocks, in fact it doesnt seem
to work well without it...

1 week class, 1 week finals, than renew visa back in germany. hell. let's
do it.
 
H

HONG

The out_of_mem() function should work when memory allocation fails via
"new" operator.

I modified a bit of the original test program as follows, and it
indeed enters the out_of_mem() function and print out the error
message.

NOTE:
Depends on how big your computer's RAM and how fast your computer is,
the program may take a couple of minutes to run into "Out of memory"
situation.

#include <new.h>
#include <iostream.h>

int out_of_mem(size_t i) {
cout << "In out_of_mem(): new failed\n";
return 0;
}

void main() {

_set_new_handler(out_of_mem);

int count = 0;
while(1) {
count++;
cout << count << endl;
int * tst = new int[16777216]; //16MB of memory
}

}

Cheers.
 

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,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top