Kelsey Bjarnason wrote:
>
> [snips]
>
> On Tue, 29 Jan 2008 10:00:39 -0800, William Ahern wrote:
>
> > But, that's beside the point. If at outset you write your code intending
> > to handle recovery, its not difficult at all. I don't remember (and
> > granted I can't remember what I wrote when I first began programming in
> > C) ever being in a situation where I found it difficult to recover or
> > unwind from a path because of a failed malloc call. Of course, I have
> > developed a very structured, almost rote method for writing software
> > which suits me. But I did so by necessity, because from very early on I
> > never accepted the premise that memory failure could or should be
> > ignored.
>
> I think that's the key to it. I keep hearing how it's so hard to check
> such things, the code is messy, yadda yadda yadda, yet it's not. Why,
> then, do some of us do this and find it relatively straightforward, while
> some don't?
It's also possible that a failed malloc() is not fatal.
I have a function which builds an index of records in a database.
The sort routine allocates memory in chunks. As long as the first
malloc() succeeds, it is irrelevent whether later mallocs() succeed
or not. As it wants more memory, it malloc()s another chunk for
its buffers, until either it has all the memory it needs to hold
all of the sort keys, hits a configurable maximum number of chunks,
or malloc() fails. At that point, it simply uses whatever memory
it allocated. A failed malloc() simply means "don't allocate any
more chunks", and is handled no differently than "I have already
allocated the maximum number of chunks I was told to allow".
[...]
--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody |
www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net |
www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <private.php?do=newpm&u=>