E
Eric
Hi What is a good way to respond to a failed Malloc (returns Null)? I do
not want to call Abort or Exit immediately.
Thanks
not want to call Abort or Exit immediately.
Thanks
Hi What is a good way to respond to a failed Malloc (returns Null)? I do
not want to call Abort or Exit immediately.
Hi What is a good way to respond to a failed Malloc (returns Null)? I do
not want to call Abort or Exit immediately.
Thanks
Well - you should probably try to flush any files which are open, return
eternal changes back to defaults (changes to the terminal etc), and find
a way of letting your user see, or at least work out why they no longer
have a running process.
Failing gracefully is a lot more effort than you might imagine at first.
Sorry maybe I wasn't too clear. I would like to get the Malloc call to
succeed if I redo it. Or get some alternative memory eg from the stack or
swop.
In said:Sorry maybe I wasn't too clear. I would like to get the Malloc call to
succeed if I redo it. Or get some alternative memory eg from the stack or
swop.
Hi What is a good way to respond to a failed Malloc (returns Null)? I do
not want to call Abort or Exit immediately.
That depends entirely upon what your program is doing. There's only one
general piece of advice that's applicable: do NOT do anything with that
null pointer value other than copying it or comparing it to other pointers.
My own code would generate an error message identifying the fact that
malloc() failed, along with the name of the function that called it, and
the name of the file containing that function. That much is required by
the coding standards for the project I'm working on. Everything else I
will say about this reflects my own personal requirements. If the
failure occurred inside a loop, the error message would indicate the
value of the loop index at the time the problem was identified. If the
message would otherwise be identical to another message that could be
produced in the same block of code, something will be added to it to
allow unique identification of the particular call to malloc() that
triggered the problem. That function would then clean up anything that
it needed to clean up, and then return an error code to the calling
function.
Hi What is a good way to respond to a failed Malloc (returns Null)? I do
not want to call Abort or Exit immediately.
Sorry maybe I wasn't too clear. I would like to get the Malloc call to
succeed if I redo it. Or get some alternative memory eg from the stack or
swop.
Hi What is a good way to respond to a failed Malloc (returns Null)? I do
not want to call Abort or Exit immediately.
I can understand this approach on a single use embedded system where
such a failure may be repeatable. Do error messages provide any useful
data on a multi-user system given the generally random memory usage
patterns?
Eric said:Hi What is a good way to respond to a failed Malloc (returns Null)? I do
not want to call Abort or Exit immediately.
While the systems my programs normally run on are nominally multi-user,
there's one user-id that is used to start the production manager
program. All other programs in the production system (including mine)
are started by the manager, directly or indirectly, and therefore all
run using the same user id. For practical purposes, it's a single-user
system (and definitely NOT an embedded one).
As I said, for failed malloc() calls the information provided by those
error messages is seldom of much use - it's just part of a general
framework for error handling that I've found useful. However, most of
the problems that come up are data-driven, and are unaffected by the
"random memory use patterns" of other programs that might be running at
the same time on the same system. For those kinds of problems, I've
found it a very useful framework.
This is example of how the right way to deal with errors depends very
much on the context in which those errors have to be dealt with, which
is precisely the point I was making.
I was recently assigned to help another group with their work - when it
fails, their software provides much less information in the error
messages than mine does. I keenly miss that information.
I understand your pain. I use a similar approach to resource allocation
failures to yours in C++ with exceptions that convey file and line
number information from the call site. These can ether propagate up or
be caught and augmented and re-thrown higher up the stack. I guess I
haven't done anything similar in C because it takes more work and lot
more discipline!
Eric said:Hi What is a good way to respond to a failed Malloc (returns Null)? I do
not want to call Abort or Exit immediately.
On 09/12/2012 06:28 PM, Ian Collins wrote:
....
On my new task, I'm actually using C++, and I've been trying to figure
out how to make use of exceptions to produce the same kind of error
output I get from the my C programs. It's a fantasy, since the new group
I'm working with has it's own well-entrenched error handling conventions
that would not mix well with my preferred style.
However, when I did indulge that fantasy, I found that one key problem
was that exceptions discard information about where they came from, as
the call stack unwinds. Unless I catch and rethrow around every function
call that could throw an exception (including the implicit function
calls due to ctors, dtors, and operator overloads), it can't duplicate
the information generated by my error messages.
This cannot be answered in general, it depends on the requirements
to the software. A similar question that might make this more clear
is to think of someone having asked »What are some good texts to
print using Printf? I do not always want to print "Hello world!".«
vaysagekv said:Is it possible to take variable length input in C.
Eric said:Sorry maybe I wasn't too clear. I would like to get the Malloc call to
succeed if I redo it. Or get some alternative memory eg from the stack or
swop.
If there's a "natural" error return, pass it up to caller. So if you're returning a pointer to dynamic memory, return null.Hi What is a good way to respond to a failed Malloc (returns Null)? I do
not want to call Abort or Exit immediately.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.