E
exits funnel
Hello,
I've written the following code:
//BEGIN CODE
#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;
class NoMemory
{
public:
NoMemory( ) { }
void* operator new(size_t sz) throw(bad_alloc);
private:
int intarr[1000];
};
void* NoMemory:perator new(size_t s) throw(bad_alloc)
{
void* add = malloc(s);
cout << "add = " << add << endl; // (1)
if (add == 0)
{
cout << "Malloc returned 0\n";
throw bad_alloc( );
}
return add;
}
int main( )
{
NoMemory* nm = 0;
try
{
while (1)
{
nm = new NoMemory;
}
}
catch(bad_alloc)
{
cerr << "Exits: Out of memory exception" << endl;
}
cout << "nm = " << nm << endl;
}
//END CODE
which compiles but doesn't behave as I would expect it to. I would
expect that to see a bunch of addresses go whizzing by until finally the
last one is zero followed on the next line by 'Exits: Out of memory
exception.' When I run it I do see the addresses go zooming by but
finally things grind to a halt and either the system just hangs or the
process goes away silently. In either case I never see the expected
'Add = 0.' I'm not sure if this is really strictly speaking a C++
question or if it is an OS question (which is redhat 8.0 for what it's
worth) but based on the code is there any reason to expect the program
to terminate in such a manner that my error handling isn't called?
Thanks in advance.
-exits
I've written the following code:
//BEGIN CODE
#include <iostream>
#include <new>
#include <cstdlib>
using namespace std;
class NoMemory
{
public:
NoMemory( ) { }
void* operator new(size_t sz) throw(bad_alloc);
private:
int intarr[1000];
};
void* NoMemory:perator new(size_t s) throw(bad_alloc)
{
void* add = malloc(s);
cout << "add = " << add << endl; // (1)
if (add == 0)
{
cout << "Malloc returned 0\n";
throw bad_alloc( );
}
return add;
}
int main( )
{
NoMemory* nm = 0;
try
{
while (1)
{
nm = new NoMemory;
}
}
catch(bad_alloc)
{
cerr << "Exits: Out of memory exception" << endl;
}
cout << "nm = " << nm << endl;
}
//END CODE
which compiles but doesn't behave as I would expect it to. I would
expect that to see a bunch of addresses go whizzing by until finally the
last one is zero followed on the next line by 'Exits: Out of memory
exception.' When I run it I do see the addresses go zooming by but
finally things grind to a halt and either the system just hangs or the
process goes away silently. In either case I never see the expected
'Add = 0.' I'm not sure if this is really strictly speaking a C++
question or if it is an OS question (which is redhat 8.0 for what it's
worth) but based on the code is there any reason to expect the program
to terminate in such a manner that my error handling isn't called?
Thanks in advance.
-exits