Re: Non-failure guarantied malloc/new
From: John Eskie (john.smith_at_intheunknown.net)
Date: 12/26/03
- Next message: Nicholas Hounsome: "Re: Non-failure guarantied malloc/new"
- Previous message: John Eskie: "Re: Non-failure guarantied malloc/new"
- In reply to: Jeff Schwab: "Re: Non-failure guarantied malloc/new"
- Next in thread: Jeff Schwab: "Re: Non-failure guarantied malloc/new"
- Reply: Jeff Schwab: "Re: Non-failure guarantied malloc/new"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Nicholas Hounsome: "Re: Non-failure guarantied malloc/new"
- Previous message: John Eskie: "Re: Non-failure guarantied malloc/new"
- In reply to: Jeff Schwab: "Re: Non-failure guarantied malloc/new"
- Next in thread: Jeff Schwab: "Re: Non-failure guarantied malloc/new"
- Reply: Jeff Schwab: "Re: Non-failure guarantied malloc/new"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|