Re: xmalloc string functions



On Tue, 29 Jan 2008 08:12:51 UTC, ymuntyan@xxxxxxxxx wrote:


There is nothing to recover because failure of fopen()
is a normal situation. Absolutely different from a failure
of malloc() when you are trying to allocate a structure
to push into the event queue to scroll text a bit later.

There is no difference at all. When I can't open a file I will tell my
caller that fact like I do when malloc() fails to delever me the
amount of memory I ask it for.

The caller will check the error condition it receives and will react
properly on that and then tell its caller the the error when it is
unable himself to recover from that error to continue as it should
when possible. Its caller does the same until there is a point that is
either
- action is complete undone - clean state
normal work goes on
- action is complete undone - no chance to to contine normal run
shut down program cleanly - a restart of the progam may be more
successfull

There is nothing that requires an abort() or exit() abnormally.

Sure, it costs a bit more to get a program failsave - but it saves
lots of money, lots of time and holds customer because no customer
will ever complain because the program aborts, looses or destroy work
or data only because the programmer was crazy enough to kill its
program only because there was a moment where an open() faild, there
was for a period of time not enough memory to allocate a small big or
very big amount of memory to get a single action done.

It makes always sense to check malloc() for success - because this
saves a lot of money for useless maintenance, earning annouyed
customers, ending up in loosing trust.




Though if all your application does is fopen(),
then you can safely abort() when fopen() fails.

Or, you could try and determine why it failed, and take corrective
action, either automatically, or at the user's direction. But hey,
that cuts into the Xbox360 time, eh?

Strawman you see. We were talking about malloc() and
you tell my application will crash when fopen() fails.

Note that if your application can ask user about something,
then it does more than fopen(). If it can take any
action when fopen() fails, then it does more. That's
what I meant. cat utility doesn't have much to do
if it can't open the file it's supposed to read.
A gui application doesn't have much to do if it
doesn't have memory to draw stuff it draws.

This is completely false. Even a guy application can handle any lack
of memory well without the need to crash the whole app.


--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
.



Relevant Pages

  • Re: style question,itoa
    ... sensible to have the caller pass in a buffer. ... technically useless malloc and free calls, ... priori when memory allocations would fail. ...
    (comp.unix.programmer)
  • Re: xmalloc string functions
    ... There is nothing to recover because failure of fopen ... of malloc() when you are trying to allocate a structure ... then you can safely abortwhen fopenfails. ... doesn't have memory to draw stuff it draws. ...
    (comp.lang.c)
  • Re: malloc does not return null when out of memory
    ... >> If malloc() fails, ... >when the program attempts to read or write to that memory, ... or caveat perhaps in the man page after "If mallocfails, ... memory allocation and usage which I think is helpful for the programmer / ...
    (freebsd-stable)
  • Re: A solution for the allocation failures problem
    ... It is not possible to check EVERY malloc result within complex software. ... possible for software quality reasons. ... a memory exhaustion situation arises, ... Crappy because a simple return NULL to the caller of the function ...
    (comp.lang.c)
  • Re: malloc behavior on failure
    ... fails, returns NULL, due to not enough resources it actually marks all ... the available memory as allocated. ... failed allocation. ... According to the C Standard, _any_ call to malloc could fail, returning ...
    (comp.lang.c)