Re: xmalloc string functions



On Thu, 31 Jan 2008 11:44:57 +0000, Richard Bos wrote:

ymuntyan@xxxxxxxxx wrote:

But it's not true, there are ifs and buts. Using glib, you *can* do
stuff when malloc() fails. Yes, using glib all you sensibly can do on
malloc() failure is some sort of emergency work and quit. If your
application requires more, then you don't want to use glib, that's it.
But I claim that for 'regular' desktop applications that is quite
enough.

If your regular desktop application is a clock, perhaps. If it holds any
user input, and for this the application can be as simple as a
calculator, it is not nearly enough. Consider how irritating it would be
to type half a dozen numbers, of twenty digits each, and a handful of
operations, only to have your application crash because it could not get
the memory for that last comlpex calculation. At the very least, your
app should put up a message box saying "Out of memory - could not
compute"

what if the system doesn't even have enough memory to pop up such a
dialog? Likely it'd take far more resources to display said dialog than
it would take to make the calculation ;-)

, and then let you copy the previous twenty-digit result into
another document for safe keeping.

ah, but that also requires a clipboard memory buffer be allocated... but
you have no memory left ;-)

Sorry to be a smart-ass here, but you clearly have not thought about this
problem in the context of the average desktop application.

In a small daemon, if you get a malloc() failure, you have a lot more
options open to you than you do in a graphical desktop application
because you can do a lot more with very small amounts of memory (or none
at all).

A few examples:

- start dropping idle client connections (which would likely not require
any /new/ allocations) until you have enough memory to do the critical
operation you need to do

- print an out-of-memory or "sorry, can't do that right now" error to the
client socket (or terminal) which would likely not require any new memory
allocations (error strings could be static)

- wait until memory becomes available


On the other hand, say, a word processor application, if the user
requests some sort of action and a malloc() fails for 12 bytes, what is
it supposed to do?

If the documents the user has open have already-opened file descriptors,
the app might be able to save them before going down - but:

1. it certainly doesn't have the option of displaying an error dialog.

2. if any of the files are unnamed or otherwise would require any of:

a) filename generation (would require string building)
b) file descriptor opening (which takes memory)
c) user-interaction (this one is right out)

the application would certainly not be able to save the documents at that
point...

3. can't simply wait because that would be a "hang" which is likely to
cause the user to kill it anyway.

But no, it crashed, taking not just
the final computation but also all intermediate results with it. Now you
have nothing, when you _could_ have had something to work from. Thank
you, glib.

Richard

Feel free to fix it?

Oh wait, I forgot that this whole thread is actually a pissing contest
more than anything else, so that people who don't actually write desktop
applications can feel superior to those who do.

Jeff
.



Relevant Pages

  • Thank You -- Thomas J. Gritzan
    ... Thomas -- Your suggestion to malloc() out a block of memory was the ... Below are some details of my memory issues ... ... As a work around solution I guessed a ram disk would solve the ... persistence will frustrate the off topic police and give them a target ...
    (comp.lang.c)
  • Re: Simple question about headers and malloc!
    ... Therefore I am making all of its declarations ... memory (using malloc) and then exit back to main. ... allocation, I get data strored from the second allocation... ...
    (microsoft.public.vc.language)
  • Re: xmalloc string functions
    ... do stuff when malloc() fails. ... using glib all you sensibly ... can do on mallocfailure is some sort of emergency work and ... the memory for that last comlpex calculation. ...
    (comp.lang.c)
  • Re: xmalloc string functions
    ... malloc() failure is some sort of emergency work and quit. ... application requires more, then you don't want to use glib, that's it. ... the memory for that last comlpex calculation. ... Some of us have and have already suggested having and using a buffer that is large enough for the emergency action. ...
    (comp.lang.c)
  • Re: ten thousand small processes
    ... Stack needs to be executable for the current signal trampoline ... the use of malloc() that is causing your primary ... if there is any heap memory in use at all, no matter what you do, ... either directly, as a 4M page mapping (not used for user processes, ...
    (freebsd-performance)