bad_alloc

G

George2

Hello everyone,


I am wondering except when there is no memory on heap, are there any
other situations when we will get bad_alloc exceptions? For example,
invalid input of the size (e.g. very huge number or zero or negative
number) will cause exception?


thanks in advance,
George
 
I

Ian Collins

George2 said:
Hello everyone,


I am wondering except when there is no memory on heap, are there any
other situations when we will get bad_alloc exceptions? For example,
invalid input of the size (e.g. very huge number or zero or negative
number) will cause exception?
Doesn't that describe exhausting the heap?
 
R

Rahul

Hello everyone,

I am wondering except when there is no memory on heap, are there any
other situations when we will get bad_alloc exceptions? For example,
invalid input of the size (e.g. very huge number or zero or negative
number) will cause exception?

thanks in advance,
George

when the user defined operator new throws bad_alloc on purpose...
 
J

jkherciueh

George2 said:
Hello everyone,


I am wondering except when there is no memory on heap, are there any
other situations when we will get bad_alloc exceptions? For example,
invalid input of the size (e.g. very huge number or zero or negative
number) will cause exception?

As far as the standard is concerned, new could throw bad_alloc randomly on
Wednesdays: the standard only says that bad_alloc indicates that the new
handler did not succeed in allocating memory, it does not specify how hard
the handler has to try. From a quality of implementation point of view,
however, you will probably not find such an implementation.


Best

Kai-Uwe Bux
 
J

James Kanze

As far as the standard is concerned, new could throw bad_alloc
randomly on Wednesdays: the standard only says that bad_alloc
indicates that the new handler did not succeed in allocating
memory, it does not specify how hard the handler has to try.
From a quality of implementation point of view, however, you
will probably not find such an implementation.

During testing, I use my own custom operator new, precisely
because it has an option which allows the test harness to
request a bad_alloc after n allocations. Unless you have
actually triggers a bad_alloc, you haven't tested any code using
dynamic allocation (and I'd argue that it is morally
irresponsible to release such code). From a quality of
implementation point of view, I'd consider such an option in the
library implementation a big plus.
 
K

kwikius

During testing, I use my own custom operator new, precisely
because it has an option which allows the test harness to
request a bad_alloc after n allocations.

It occurs to me that an ideal test implementation should also provide
an option to make sure you really are out of memory then throw
bad_alloc, ideally using various mechanisms.

regards
Andy Little
 

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

Similar Threads


Members online

Forum statistics

Threads
473,780
Messages
2,569,607
Members
45,240
Latest member
pashute

Latest Threads

Top