473,414 Members | 1,596 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,414 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 1575
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?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.