473,395 Members | 1,458 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,395 software developers and data experts.

Exception checking memory allocation. How?

It is common practice (I've heard) to always catch
allocation exceptions from "new". How is done in
practice? What would be a common procedure (path of
sequence) after such an event has occured? (For a
multi-million LOC, GUI based application).

It is also a common advice not to allocate objects
with "new" unless absolutely required. The alternative
is to keep objects on the stack, automatically allocated
and deallocated by scope control. Which mechanisms do
I use in order to prevent allocation errors (i.e. stack
overflow) in this case?

Thanks!
Jul 30 '05 #1
16 2476
> It is common practice (I've heard) to always catch
allocation exceptions from "new". How is done in
practice? What would be a common procedure (path of
sequence) after such an event has occured? (For a
multi-million LOC, GUI based application).
I don't know about commonality but allocating the exception object thrown on
the heap really doesn't seem to have any advantages. You won't have a C++
exception for stack overflow. And besides, you have to delete the thrown
exception if it is newed in a context where a garbage collector is absent.

Most of the time it is the type the object thrown that is important. Only
occasionally I have to check the internal information carried by the
exception thrown.

It is also a common advice not to allocate objects
with "new" unless absolutely required. The alternative
is to keep objects on the stack, automatically allocated
and deallocated by scope control. Which mechanisms do
I use in order to prevent allocation errors (i.e. stack
overflow) in this case?


It really depends on how the object is to be used. Here are some facts:
*The stack is popped off when the execution goes out of scope, so you can
preserve anything on the stack beyond the scope. However, you don't have to
worry about manually deleting the object.
*Stack allocation is much more efficient than other means in most systems.
*The stack is limited, often much smaller than max heap you can acquire
mostly. For small objects, this isn't an issue. But for large objects it
should be taken care of. A common practice is to utilize RAII (for example,
std::string)

Ben
Jul 30 '05 #2
> It really depends on how the object is to be used. Here are some facts:
*The stack is popped off when the execution goes out of scope, so you can
preserve anything on the stack beyond the scope. However, you don't have to worry about manually deleting the object.


I meant you CANNOT preserve anything on the stack beyond the scope of
course. Excuse my typo.

Ben
Jul 30 '05 #3
Jacob wrote:
It is common practice (I've heard) to always catch
allocation exceptions from "new".
Not always. In a small program used in a environment when enough memory is
always available, and wit users with some commnon sense, you can just let
the program abort.
How is done in practice? What would be a common procedure (path of
sequence) after such an event has occured? (For a multi-million LOC,
GUI based application).
The common procedure is to do something according to the guidelines written
by the designers of the project.
It is also a common advice not to allocate objects
with "new" unless absolutely required. The alternative
is to keep objects on the stack, automatically allocated
and deallocated by scope control. Which mechanisms do
I use in order to prevent allocation errors (i.e. stack
overflow) in this case?


Allocate objetcs with new when required, and forget the word "absolutely".
Study the limitations of your platform and his non-standard ways of
checking it.

--
Salu2
Jul 30 '05 #4
Julián Albo wrote:
The common procedure is to do something according to the guidelines written
by the designers of the project.
Well, that is me in this case, so therefore I go to
the gurus for advice :-)
Allocate objetcs with new when required, and forget the word "absolutely".
Study the limitations of your platform and his non-standard ways of
checking it.


My platform is not necesserily my customers platform.
Hardware varies. And crashing is _not_ an option.
Jul 30 '05 #5
> My platform is not necesserily my customers platform.
Hardware varies. And crashing is _not_ an option.


All software crashes at some point...

Jul 30 '05 #6
Jacob wrote:
Allocate objetcs with new when required, and forget the word
"absolutely". Study the limitations of your platform and his non-standard
ways of checking it.

My platform is not necesserily my customers platform.
Hardware varies. And crashing is _not_ an option.


Study the limitations of your customer's platforms and his non-standard
ways of checking it ;)

--
Salu2
Jul 30 '05 #7
"Jacob" <ja***@yahoo.com> wrote in message
news:2Z********************@telenor.com...
It is common practice (I've heard) to always catch
allocation exceptions from "new". How is done in
practice? What would be a common procedure (path of
sequence) after such an event has occured? (For a
multi-million LOC, GUI based application).
In practice, you first of all make sure that your code
is exception-safe. That is, if an exception is thrown,
the current operation is stopped or cancelled gracefully.
In particular, you should use RAII to avoid memory
or resource leaks.

Then you should only need to care about error handling at
the top-level of a program (e.g. the agent that generates
commands, or top-level functions that handle user-
generated commands). Only there do you care and know how
to report the failure of an operation.
Sometimes, at an intermediate level, it is possible
to release resources or reset/alter some environment
parameters to attempt the same operation again.
It is also a common advice not to allocate objects
with "new" unless absolutely required. The alternative
is to keep objects on the stack, automatically allocated
and deallocated by scope control. Which mechanisms do
I use in order to prevent allocation errors (i.e. stack
overflow) in this case?


What is 'absolutely' required?
Objects should be allocated dynamically when it is
suitable to manually control their lifetime, and stack-
based or global objects are not an adequate solution.
This is a matter of design.
Also, dynamic allocation does not imply using 'new'
(think in terms of containers when possible...).

When a stack can overflow and how this error condition
is handled is unfortunately a platform-specific detail.
There is no fully portable solution in standard C++.

Sometimes large objects or data stacks are indeed
allocated on the heap just because this provides better
control over object allocations and error handling
(e.g. a recursive algorithm can be rewritten to use
an std::vector rather than the program stack, to better
detect and handle excessive recursion).
I hope this helps,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com


Jul 30 '05 #8
Jacob sade:
What would be a common procedure (path of
sequence) after such an event has occured?


GUI::DisplayAndForceExit("Internal run-time error. Auto-shutdown
commencing. All unsaved data will be lost. Be well!");

:)

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Jul 30 '05 #9
Tobias Blomkvist wrote:
What would be a common procedure (path of
sequence) after such an event has occured?

GUI::DisplayAndForceExit("Internal run-time error. Auto-shutdown
commencing. All unsaved data will be lost. Be well!");


Why do you suggest a shutdown?

You just encouter a memory allocation failure, but the program
should be able to continue. Locally, the allocating class should
handle the problem (technically how, I haven't got an answer for yet),
and at the application level the event should be flagged to the GUI
somehow. Following my company guidelines inter-library exception
throw/catch is not an option, so I need to use other mechanisms.
Jul 30 '05 #10
Jacob wrote:
somehow. Following my company guidelines inter-library exception
throw/catch is not an option, so I need to use other mechanisms.


But wasn't you who were deciding the guidelines?

--
Salu2
Jul 30 '05 #11
Julián Albo wrote:
somehow. Following my company guidelines inter-library exception
throw/catch is not an option, so I need to use other mechanisms.

But wasn't you who were deciding the guidelines?


Yes. And according to C++ best practices, inter-library
exceptions is not adviced.
Jul 30 '05 #12
Jacob wrote:
somehow. Following my company guidelines inter-library exception
throw/catch is not an option, so I need to use other mechanisms.

But wasn't you who were deciding the guidelines?

Yes. And according to C++ best practices, inter-library
exceptions is not adviced.


Is "C++ best practices" the name of your company, then?

--
Salu2
Jul 30 '05 #13
Jacob sade:
You just encouter a memory allocation failure, but the program
should be able to continue.


What if the memory allocation failure isn't just a single event,
but a complete process memory failure, what are your options then?
Can your application react and save necessary, perhaps crucial, data
without the need for more heap memory?

My experience is that most programs just crash with a curious message,
sometimes generated from the dark depths of the code structure,
hence my first answer. Memory allocation control is a rare thing.

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Jul 30 '05 #14
Tobias Blomkvist wrote:
My experience is that most programs just crash with a curious message,
sometimes generated from the dark depths of the code structure,


Commonly known as bad design.

Note that I am not talking about memory errors (which are bugs),
but rather resource limitation (which are predictable and can be
accounted for).

Industry level (Microsoft not included) software must check
the result of *every* memory allocation made and act appropriately
for every possible outcome; Again: Crashing is not an option!

This is not really hard. It is more like a pattern. And I like
advice on common practice.

But maybe weekends are a bad time... :-)
Jul 30 '05 #15
Jacob sade:
Note that I am not talking about memory errors (which are bugs),
but rather resource limitation (which are predictable and can be
accounted for).
I wasn't speaking of bugs introduced by you as a programmer in your
own code.

If your system suddenly works against you, changes in resource
limitations could wreak havoc concerning memory and resource needs.

Unless you're designing the entire execution environment, somebody
else debugs a system which you rely on, and many systems can fault.

Industry level (Microsoft not included) software must check
the result of *every* memory allocation made and act appropriately
for every possible outcome; Again: Crashing is not an option!
Yes, how would you otherwise come to the conclusion that further
memory allocations might be impossible and that the severe failure
in memory allocations might raise the need of a controlled restart,
the question is just how clean you can do it, and what help your
system can give you? Restart(either process restart or context restart),
not crash. This is yet a reason why gui and core(s) should be separated
so individual restarts can be done.
This is not really hard. It is more like a pattern. And I like
advice on common practice.
Like isolating functionality to control memory mishaps. If a memory
failure (or any failure which is not related to your coding skills)
occurs in context C then terminate C (and retry). This means a design
where arguments to C will not be changed. Allocate everything within
C from a memory "pool" outside C which can easily be deallocated upon
failure, hence disabling the need for complex deallocation procedures
within C. This gives you control over defined contexts with ease. If
vital contexts repeatly fails, then you might want to think on plan b.

(Context-Oriented Programming =)

You're right, it isn't that hard to control if you are aware.
But maybe weekends are a bad time... :-)


Maybe I should take that as an insult :-)

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
Jul 30 '05 #16

"Jacob" <ja***@yahoo.com> wrote in message
news:MM********************@telenor.com...
somehow. Following my company guidelines inter-library exception
throw/catch is not an option, so I need to use other mechanisms.


this is a stupid nonsensical restriction -- exceptions are intended for
reporting errors out of libraries
Jul 31 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: kwijibo28 | last post by:
Hi all, I've got a simple question regarding stl containers. Consider this code: std::vector<float> foo; foo.resize(100); How do I know if memory allocation was successful? The resize...
2
by: CoolPint | last post by:
Standard exception classes in C++ have what() which returns const char * and they have constructors accepting string. Where is that string created and when is the string destroyed? In the...
21
by: Stephan | last post by:
why does the following code not work???? after compiling and running it will just say killed after all my memory filled up any suggestions? #include <iostream> using namespace std; void...
10
by: Steven T. Hatton | last post by:
I read Stroustrup's article of the day: http://www.research.att.com/~bs/C++.html Programming with Exceptions. InformIt.com. April 2001. http://www.research.att.com/~bs/eh_brief.pdf Some of...
3
by: Tony Johansson | last post by:
Hello! When you allocate object dynamically which mean on the heap I find that a problem when using exception. What is the prefer method to handle this kind of problem. //Tony
6
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the...
99
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
66
by: Johan Tibell | last post by:
I've written a piece of code that uses sockets a lot (I know that sockets aren't portable C, this is not a question about sockets per se). Much of my code ended up looking like this: if...
24
by: Ken | last post by:
In C programming, I want to know in what situations we should use static memory allocation instead of dynamic memory allocation. My understanding is that static memory allocation like using array...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.