Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #26262

Re: Failing Malloc

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Failing Malloc
Date 2012-09-12 17:20 -0400
Organization A noiseless patient Spider
Message-ID <5050FC9D.4010608@verizon.net> (permalink)
References <k2qq2i$aba$1@speranza.aioe.org>

Show all headers | View raw


On 09/12/2012 04:07 PM, Eric wrote:
> 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.

The calling function would respond to that error code by generating
another error message containing the same type of information for the
calling function as was described above for the function in which the
problem was originally detected. This continues all the way up the call
chain to main(), which would generate a final error message before
returning EXIT_FAILURE. The net effect is an annotated reverse
stack-trace. That information isn't particularly useful when dealing
with malloc() failures, but it's part of a general architecture for
dealing with error conditions that helps me get a running start on
diagnosing the underlying problem.

This is a totally inappropriate way of handling error conditions for
many contexts, such as interactive GUI programs. However, most of my
code exclusively runs inside a batch processing environment, where this
is quite a reasonable approach.

You need to figure out the right approach for your purposes.

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Failing Malloc Eric <nospam@nospam.com> - 2012-09-12 20:07 +0000
  Re: Failing Malloc Xavier Roche <xroche@free.fr.NOSPAM.invalid> - 2012-09-12 22:12 +0200
    Re: Failing Malloc William Ahern <william@wilbur.25thandClement.com> - 2012-09-12 14:25 -0700
  Re: Failing Malloc Andrew Cooper <andrew@nospam.example.com> - 2012-09-12 21:21 +0100
    Re: Failing Malloc Eric <nospam@nospam.com> - 2012-09-12 20:34 +0000
      Re: Failing Malloc Paul N <gw7rib@aol.com> - 2012-09-12 13:46 -0700
        Re: Failing Malloc Eric <nospam@nospam.com> - 2012-09-13 20:21 +0000
          Re: Failing Malloc Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-13 17:31 -0400
          Re: Failing Malloc James Kuyper <jameskuyper@verizon.net> - 2012-09-13 17:57 -0400
      Re: Failing Malloc John Gordon <gordon@panix.com> - 2012-09-12 20:55 +0000
        Re: Failing Malloc William Ahern <william@wilbur.25thandClement.com> - 2012-09-12 14:33 -0700
        Re: Failing Malloc falk@rahul.net (Edward A. Falk) - 2012-09-13 22:43 +0000
      Re: Failing Malloc James Kuyper <jameskuyper@verizon.net> - 2012-09-12 17:37 -0400
      Re: Failing Malloc Johann Klammer <klammerj@NOSPAM.a1.net> - 2012-09-13 08:57 +0200
  Re: Failing Malloc James Kuyper <jameskuyper@verizon.net> - 2012-09-12 17:20 -0400
    Re: Failing Malloc Ian Collins <ian-news@hotmail.com> - 2012-09-13 09:27 +1200
      Re: Failing Malloc James Kuyper <jameskuyper@verizon.net> - 2012-09-12 17:56 -0400
        Re: Failing Malloc Ian Collins <ian-news@hotmail.com> - 2012-09-13 10:28 +1200
          Re: Failing Malloc James Kuyper <jameskuyper@verizon.net> - 2012-09-12 18:57 -0400
            Re: Failing Malloc William Ahern <william@wilbur.25thandClement.com> - 2012-09-12 16:21 -0700
            Re: Failing Malloc Ian Collins <ian-news@hotmail.com> - 2012-09-13 12:07 +1200
              Re: Failing Malloc James Kuyper <jameskuyper@verizon.net> - 2012-09-13 13:18 -0400
                Re: Failing Malloc Ian Collins <ian-news@hotmail.com> - 2012-09-14 07:37 +1200
                Re: Failing Malloc James Kuyper <jameskuyper@verizon.net> - 2012-09-13 16:05 -0400
                Re: Failing Malloc William Ahern <william@wilbur.25thandClement.com> - 2012-09-13 14:21 -0700
                Re: Failing Malloc James Kuyper <jameskuyper@verizon.net> - 2012-09-13 17:38 -0400
                Re: Failing Malloc William Ahern <william@wilbur.25thandClement.com> - 2012-09-13 14:03 -0700
                Re: Failing Malloc Ian Collins <ian-news@hotmail.com> - 2012-09-15 10:01 +1200
            Re: Failing Malloc Philip Lantz <prl@canterey.us> - 2012-09-14 02:50 -0700
  Re: Failing Malloc Kaz Kylheku <kaz@kylheku.com> - 2012-09-12 21:36 +0000
  Re: Failing Malloc Eric Sosman <esosman@ieee-dot-org.invalid> - 2012-09-12 17:41 -0400
  Re: Failing Malloc Joe Pfeiffer <pfeiffer@cs.nmsu.edu> - 2012-09-12 16:07 -0600
    Re: Failing Malloc "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-09-12 16:03 -0700
  Re: Failing Malloc Kenneth Brody <kenbrody@spamcop.net> - 2012-09-12 19:24 -0400
  Re: Failing Malloc The Great Firewall of China Blue <chine.bleu@yahoo.com> - 2012-09-12 16:34 -0700
  Re: Failing Malloc Malcolm McLean <malcolm.mclean5@btinternet.com> - 2012-09-13 00:14 -0700

csiph-web