Re: Non-failure guarantied malloc/new

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 12/24/03


Date: Wed, 24 Dec 2003 22:10:29 GMT


"John Eskie" <john.smith@intheunknown.net> wrote...
> Lately I've seen alot of C and C++ code (not my own) which doesn't do any
> checking if memory obtained by new or malloc is valid or if they return
NULL
> pointers.
> Why does most people not care about doing this kind of error checking?

Probably because memory is cheap and computers on which the programs made
from the code you've seen are supposed to have lots of it. Just a guess...

> When I used to learn the language I was told always to check "if (p !=
> NULL)" and return a error otherwise.

It's not a bad style.

> I still do that always but sometimes it's annoying the hell out of me if I
> want to allocate memory inside a constructor and have no way to return an
> error code to the caller to inform that something went wrong.

Throw an exception. It's the usual way to indicate a failure to create
an object.

> Thinking about this issue made me do some hacks like running memory
> allocation in a while() loop such as:
>
> blah *p = NULL;
> while (!p)
> {
> p = malloc(...);
> }

To do what?

> On top of the whole thing we got new operator which works differently on
> each compiler implementation. Some compilers throw exceptions while others
> give NULL pointers.

Those that give NULL pointers are non-compliant.

> Actually I prefer NULL pointers because it's so much
> easier to handle in a while loop then doing some exception handling.

Then you need to use a "nothrow" form of the new operator.

> I hope I've reached my point now because OS's do run out of memory. Why
> would I let my program crash if I can avoid it? Just for fun I recently
> wrote a program that took all my memory in the OS and then started one
> program which I knew did not have any error checking on new operator.
Guess
> what, it crashes in no-time.

Sure. What else could it do?

> In my opinion it's still better to run a while() loop and stall the
program
> then having it crash instead, no?

I am not sure what you mean by that. I get really annoyed by programs
that do not respond when I need them to, and have to kill them manually.
How is that better than crashing?

> I am looking for suggestions or possible implementations of memory
> allocation which is guarrantied not to fail.

No such thing. Since memory is a limited resource, some memory
allocations are bound to fail.

Victor



Relevant Pages

  • Re: What is the advantage of malloc over calloc
    ... Unless of course the zeroed memory was to hold a set of pointers and a ... than null pointers and 0.0 floats. ... zeroing in a memory allocation call. ... performance-critical area there are likely other more significant ...
    (comp.lang.c)
  • Re: How java passes object references?
    ... Constructs a new `Human' object, preinitializes it, calls the constructor, and then marks the local variable register as pointing to it. ... two memory blocks for each object (the one from main and the one from ... the heap, so the term `allocation' doesn't apply there, but otherwise correct. ... how pointers work in memory. ...
    (comp.lang.java.programmer)
  • Re: Is this math test too easy?
    ... communications glitch; one of the more laughable cartoons ... (A note on "in memory", which for virtual memory machines can get ... Or one can interpret the character string as one of the values ... DOS allowed NULL pointers; the value was in fact interpreted as ...
    (sci.math)
  • [PATCH 2.6.17-rc4 2/6] Some documentation for kmemleak
    ... +Kernel Memory Leak Detector ... +with the difference that the orphan pointers are not freed but only ... +An allocated block of memory is considered orphan if a pointer to its ... The memory allocations hook stores the pointer address together ...
    (Linux-Kernel)
  • [PATCH 2.6.17-rc5 2/7] Some documentation for kmemleak
    ... +Kernel Memory Leak Detector ... +with the difference that the orphan pointers are not freed but only ... +An allocated block of memory is considered orphan if a pointer to its ... The memory allocations hook stores the pointer address together ...
    (Linux-Kernel)

Loading