473,219 Members | 1,637 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,219 software developers and data experts.

new operator

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
6 1568
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paul Davis | last post by:
I'd like to overload 'comma' to define a concatenation operator for integer-like classes. I've got some first ideas, but I'd appreciate a sanity check. The concatenation operator needs to so...
30
by: | last post by:
I have not posted to comp.lang.c++ (or comp.lang.c++.moderated) before. In general when I have a C++ question I look for answers in "The C++ Programming Language, Third Edition" by Stroustrup....
1
by: joesoap | last post by:
Hi can anybody please tell me what is wrong with my ostream operator??? this is the output i get using the 3 attached files. this is the output after i run assignment2 -joesoap #include...
0
by: Martin Magnusson | last post by:
I have defined a number of custom stream buffers with corresponding in and out streams for IO operations in my program, such as IO::output, IO::warning and IO::debug. Now, the debug stream should...
6
by: YUY0x7 | last post by:
Hi, I am having a bit of trouble with a specialization of operator<<. Here goes: class MyStream { }; template <typename T> MyStream& operator<<(MyStream& lhs, T const &)
3
by: gugdias | last post by:
I'm coding a simple matrix class, which is resulting in the following error when compiling with g++ 3.4.2 (mingw-special): * declaration of `operator/' as non-function * expected `;' before '<'...
5
by: raylopez99 | last post by:
I need an example of a managed overloaded assignment operator for a reference class, so I can equate two classes A1 and A2, say called ARefClass, in this manner: A1=A2;. For some strange reason...
8
by: valerij | last post by:
Yes, hi How to write "operator +" and "operator =" functions in a class with a defined constructor? The following code demonstrates that I don't really understand how to do it... I think it has...
3
by: y-man | last post by:
Hi, I am trying to get an overloaded operator to work inside the class it works on. The situation is something like this: main.cc: #include "object.hh" #include "somefile.hh" object obj,...
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.