overloaded operator new

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::eek: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
 
P

Pete Becker

exits said:
cout << "add = " << add << endl; // (1)

Don't insert into streams in operator new. They often allocate memory,
using operator new, and you end up in a death spiral. Use printf
instead.
 
R

Rolf Magnus

Pete said:
Don't insert into streams in operator new. They often allocate memory,
using operator new, and you end up in a death spiral. Use printf
instead.

And why would the streams use NoMemory::eek:perator new for that?
 
R

Ron Natalie

Pete Becker said:
Don't insert into streams in operator new. They often allocate memory,
using operator new, and you end up in a death spiral. Use printf
instead.

And you'd better hope that stdout has already allocated it's buffer (probably
a safe bet)...
 
R

Ron Natalie

Rolf Magnus said:
And why would the streams use NoMemory::eek:perator new for that?
It's got nothing to do with NoMemory::eek:perator new. His test case malloc's memory until
MALLOC itself fails. Once malloc starts failing, a lot of library calls are going to start
exhibiting strange behavior.
 
E

exits funnel

Pete,

This was a good suggestion. Things still don't turn out like I'd expect
but at least I'm one step closer. I'm going to try to figure out how
to decrease the heap available to the process so that at least I can
work on this without crashing my system :) In any case, that is
off-topic for this group. Thanks again.
 
P

Pete Becker

exits said:
This was a good suggestion. Things still don't turn out like I'd expect
but at least I'm one step closer. I'm going to try to figure out how
to decrease the heap available to the process so that at least I can
work on this without crashing my system :) In any case, that is
off-topic for this group. Thanks again.

Try something like this:

unsigned long max = 10000;
unsigned long alloced = 0;

void *wrap_malloc(size_t sz)
{ /* allocate sz bytes, but don't exceed max total bytes */
void *res = 0;
if (sz <= max && alloced <= max - sz && (res = malloc(sz)) != 0)
alloced += sz;
return res;
}

From your operator new call wrap_malloc instead of malloc.
 

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
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top