new operator

  • Thread starter Christian Christmann
  • Start date
C

Christian Christmann

Hi,

a general question on the "new" operator.
Creating object dynamically using "new" might lead to problems,
when e.g. not sufficient memory is available and the object could
not be created on the heap. Shouldn't thus not all "new" usages
be included in a try{} block in order to enable handling of potential
problems?
However, their must be reasons why this is not done since lots of
people just use "new" without any corresponding exception
handling. Or is this just laziness? :)

Regards,
Chris
 
?

=?ISO-8859-15?Q?Juli=E1n?= Albo

Christian said:
Creating object dynamically using "new" might lead to problems,
when e.g. not sufficient memory is available and the object could
not be created on the heap. Shouldn't thus not all "new" usages
be included in a try{} block in order to enable handling of potential
problems?

No. For example, if the program is simple, let it to atutomatically call
terminate if there is not enough memory is perfectly acceptable (provided
the intended users are not dummies).

And in many cases you must not catch the exception if you can't do something
reasonable whit it, the caller may know what to do. The code must be
exception safe, that is, let all things in a reasonable state when an
exception is thrown.
 
A

Andrzej 'Rudy' D.

Christian Christmann narzeźbił(a):
Hi,

a general question on the "new" operator.
Creating object dynamically using "new" might lead to problems,
when e.g. not sufficient memory is available and the object could
not be created on the heap. Shouldn't thus not all "new" usages
be included in a try{} block in order to enable handling of potential
problems?

Take note of the standard std::set_new_handler function.
 
F

Frederick Gotham

Christian Christmann posted:

However, their must be reasons why this is not done since lots of
people just use "new" without any corresponding exception
handling. Or is this just laziness? :)


If I were allocating megabytes, then I'd error-check.

If I were allocating less than a kilobyte in a large GUI program, then I
wouldn't bother error-checking -- just let it crash and burn if it can't find
a miserable kilobyte to spare. (And by that time, the PC will have grinded to
a halt and will be doing some pretty noisy disk thrashing).
 
J

Jim Langston

Christian Christmann said:
Hi,

a general question on the "new" operator.
Creating object dynamically using "new" might lead to problems,
when e.g. not sufficient memory is available and the object could
not be created on the heap. Shouldn't thus not all "new" usages
be included in a try{} block in order to enable handling of potential
problems?
However, their must be reasons why this is not done since lots of
people just use "new" without any corresponding exception
handling. Or is this just laziness? :)

Regards,
Chris

Most of the time if I can't new enough memory for what I needed, there would
be nothing useful I could do other than display an error saying I was out of
memory and terminate. Which is what the default handler is going to do
anyway.

If, on the other hand, there is something I can do if I can't allocate
enough memory, maybe inform the user that there is not enough memory to load
what they are trying to load, try loading something else, then I would put
it in a try...catch() block.
 
M

Michiel.Salters

Christian said:
Hi,

a general question on the "new" operator.
Creating object dynamically using "new" might lead to problems,
when e.g. not sufficient memory is available and the object could
not be created on the heap. Shouldn't thus not all "new" usages
be included in a try{} block in order to enable handling of potential
problems?

For reliable programs, yes. Of course, a single try { } in main() does
include most of those cases. The only exceptions are calls to new in
constructors of globals, and those shouldn't use too much memory
anyway.

HTH,
Michiel Salters
 
R

Ron Natalie

Christian said:
Hi,

a general question on the "new" operator.
The question is, if new fails, what are you going to do
about it? In 99% of the time, you're not prepared to
handle new failures because unless you're very careful
anything that will handle it will also do something that
might allocate more memory.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top