By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,227 Members | 1,315 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,227 IT Pros & Developers. It's quick & easy.

overloaded operator new

P: n/a
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::operator 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

Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
exits funnel wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #2

P: n/a
Pete Becker wrote:
exits funnel wrote:

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.


And why would the streams use NoMemory::operator new for that?

Jul 22 '05 #3

P: n/a

"Pete Becker" <pe********@acm.org> wrote in message news:3F***************@acm.org...
exits funnel wrote:

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.


And you'd better hope that stdout has already allocated it's buffer (probably
a safe bet)...
Jul 22 '05 #4

P: n/a

"Rolf Magnus" <ra******@t-online.de> wrote in message news:br*************@news.t-online.com...
Pete Becker wrote:
exits funnel wrote:

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.


And why would the streams use NoMemory::operator new for that?

It's got nothing to do with NoMemory::operator 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.

Jul 22 '05 #5

P: n/a
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.

Pete Becker wrote:
exits funnel wrote:
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.


Jul 22 '05 #6

P: n/a
exits funnel wrote:

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.