Re: Non-failure guarantied malloc/new

From: John Eskie (john.smith_at_intheunknown.net)
Date: 12/26/03


Date: Fri, 26 Dec 2003 09:46:29 +0100


"Jeff Schwab" <jeffplus@comcast.net> wrote in message
news:R-KdnVPKtZtijXeiRVn-tw@comcast.com...

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

In past yes. But I've also used this construct for C++ applications.

> > 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.
>
As written in another post I am unsure how to write a proper handler for it.
After all I can recieve such a exception at any time so I will have no
possibilities to do proper cleanup of resources.

> > In my opinion it's still better to run a while() loop and stall the
program
> > then having it crash instead, no?
>
> I suppose so, but I don't really like either of those options. If
> availability of the process is not cricital, I usually print an error
> message, then try to exit as gracefully as possible. Of course, if it's
> essential that the program not die, the resources needed must be
> specified very carefully, and the program can try to reserve them all on
> startup.

Yes but if you got some open file handles or similar then you can't really
clean up the whole thing gracefully. I would say there is alot effort to do
that. This is probably also the reason why I dislike exceptions. Because you
end up somewhere in the program and have no way of going back. If you just
get a NULL pointer you can check for you will be able to return to the
caller and inform there is something wrong and the caller can do it's own
cleanup.

> > I am looking for suggestions or possible implementations of memory
> > allocation which is guarrantied not to fail.
>
> You could allocate all needed memory in static blocks. Of course, then
> every run of your program will use as much memory as the worst case.
> Your while-loop idea may be the next closest thing to a "fail-proof"
> allocator.
>
While it's certainly possible it's very impractical. Sometimes you don't
know how much memory you will need. A simple example is when you read in a
file of size which you find out when the program runs.

-- John



Relevant Pages

  • Re: Pointer to the out of scope local variables
    ... of a pointer, and jumped to the conclusion that you allocated the memory ... In this particular case the funtion buildPoint would allocate ... If the caller ...
    (microsoft.public.vc.language)
  • Re: Technical two dimensional array question
    ... > Does it allocate a contiguous blocks of memory or not? ... What it allocates is memory space for *references* to string objects, ... > I tried it and I get an exception error, as I should, but, ... allocated array. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Pointer to the out of scope local variables
    ... Thus he probably saw the signature of buildPoint, saw the return of a pointer, and jumped to the conclusion that you allocated the memory for the return value _dynamically_. ... In this particular case the funtion buildPoint would allocate the memory but the calling function had to free the memory. ... If the caller forgets to do so, ...
    (microsoft.public.vc.language)
  • Re: new keyword
    ... all required memory (what happens or which exception is thrown depends ... configured to have "new" return NULL if it fails to allocate. ... by the underlying object's constructor will be destructed, ... memory allocated by "new" will be freed. ...
    (microsoft.public.vc.mfc)
  • Re: new revision ada
    ... > of additional memory, too. ... Even if it isn't the out of memory exception ... model would have to allocate memory. ... on the exception message string is simply so that memory doesn't need to be ...
    (comp.lang.ada)