473,794 Members | 2,752 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2520
> 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.co m> wrote in message
news:2Z******** ************@te lenor.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::DisplayAnd ForceExit("Inte rnal 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::DisplayAnd ForceExit("Inte rnal 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

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

Similar topics

6
13879
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 method returns nothing. I understand that resize does not always allocate
2
2423
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 function below, e is a local object so when the function terminates, the internal string should be gone too, isn't it?
21
7288
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 out_of_mem() {
10
2058
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 these ideas are finally beginning to sink in. I believe I looked at the same article a while back and decided I wasn't quite ready for it. If I understood things correctly, there seems to be a slight problem with the design of his exception safe...
3
2255
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
2702
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 memory allocation algorithm, but for gathering some statistics and to find memory leaks.) This seems to work. However, I noticed that my replacing delete operator is not called
99
5201
by: Mikhail Teterin | last post by:
Hello! Consider the following simple accessor function: typedef struct { int i; char name; } MY_TYPE; const char *
66
3645
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 (function(socket, args) == -1) { perror("function"); exit(EXIT_FAILURE); } I feel that the ifs destroy the readability of my code. Would it be
24
19097
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 is faster than malloc, but dynamic memory allocation is more flexible. Please comment... thanks.
0
9518
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10212
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10000
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9035
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5560
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4112
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.