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
| > | >> 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