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

new operator

P: n/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?
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
Jul 11 '06 #1
Share this Question
Share on Google+
6 Replies


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

--
Pozdrawiam, Andrzej
Jul 11 '06 #2

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

--
Salu2
Jul 11 '06 #3

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

Frederick Gotham
Jul 12 '06 #4

P: n/a
"Christian Christmann" <pl*****@yahoo.dewrote in message
news:44***********************@newsread4.arcor-online.net...
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.
Jul 12 '06 #5

P: n/a
Christian Christmann wrote:
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

Jul 12 '06 #6

P: n/a
Christian Christmann wrote:
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.
Jul 12 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.