Re: Malcolm's new book - Chapter 1 review
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Sat, 25 Aug 2007 21:14:46 -0400
Keith Thompson wrote:
CBFalconer <cbfalconer@xxxxxxxxx> writes:
Flash Gordon wrote:[...]
[...]Keith's point is that if the user of the library function could
specify a maximum size (possibly 0 meaning unlimited) then the
user of the library function could decide on some suitable upper
bound.
I wrote ggets{} to replace gets{}. It maintains the simplicity -
you supply only the address of a pointer, which will receive the
pointer to the next input line. The only other thing to worry
about is the return value, which can be 0 (good), EOF (EOF) or
positive non-zero (I/O error). Now you have to remember to arrange
to free() that pointer at some time. You can also copy it
elsewhere, embed it in a linked list, etc. etc.
However, use is always totally safe. The input action will never
overwrite anything. If you put any limits on it, sooner or later
those will bite. Or they are one more parameter to "get right"
before calling. The simplest parameter is no parameter. It is
fairly hard to get that one wrong.
A program using ggets(), reading from an arbitrary input file, can
attempt to allocate an arbitrarily large amount of memory. It will
eventually fail cleanly (assuming malloc and realloc work the way
they're required to), but even so, allocating as much memory as you
can may have negative consequences. I can write a program that calls
malloc() in a loop to see how much I can allocate, but I wouldn't want
to run it on a shared system.
I'm not suggesting changing the default behavior, just providing a way
for the user to change it. You could either provide a routine to set
a maximum size for future calls (though that could introduce issues
for threaded environments), or provide an additional function that
lets you specify a limit. (The behavior on exceeding the limit would
have to be defined.)
Heck, since it's public domain, I might go ahead and make some changes
myself. Naturally you're under no obligation to accept them; and if I
distribute it myself I'll certainly give you credit. I'll do this in
my copious free time, of course, so don't hold your breath.
Since it is PD you can do whatever you wish. However I request
that, if you change the header file in any way, you also change the
routines name.
Note the simplicity and safety of the demo file reverse.c.
--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
--
Posted via a free Usenet account from http://www.teranews.com
.
- Follow-Ups:
- Re: Malcolm's new book - Chapter 1 review
- From: Keith Thompson
- Re: Malcolm's new book - Chapter 1 review
- References:
- Re: Malcolm's new book
- From: Malcolm McLean
- Re: Malcolm's new book
- From: Kelsey Bjarnason
- Re: Malcolm's new book
- From: Richard Heathfield
- Re: Malcolm's new book
- From: Kelsey Bjarnason
- Re: Malcolm's new book
- From: Malcolm McLean
- Re: Malcolm's new book - Chapter 1 review
- From: Eric Sosman
- Re: Malcolm's new book - Chapter 1 review
- From: Malcolm McLean
- Re: Malcolm's new book - Chapter 1 review
- From: Richard Heathfield
- Re: Malcolm's new book - Chapter 1 review
- From: pete
- Re: Malcolm's new book - Chapter 1 review
- From: Richard Heathfield
- Re: Malcolm's new book - Chapter 1 review
- From: Philip Potter
- Re: Malcolm's new book - Chapter 1 review
- From: Richard Heathfield
- Re: Malcolm's new book - Chapter 1 review
- From: Malcolm McLean
- Re: Malcolm's new book - Chapter 1 review
- From: CBFalconer
- Re: Malcolm's new book - Chapter 1 review
- From: Keith Thompson
- Re: Malcolm's new book - Chapter 1 review
- From: Richard Bos
- Re: Malcolm's new book - Chapter 1 review
- From: Keith Thompson
- Re: Malcolm's new book - Chapter 1 review
- From: Malcolm McLean
- Re: Malcolm's new book - Chapter 1 review
- From: Flash Gordon
- Re: Malcolm's new book - Chapter 1 review
- From: CBFalconer
- Re: Malcolm's new book - Chapter 1 review
- From: Keith Thompson
- Re: Malcolm's new book
- Prev by Date: Re: Function prototypes
- Next by Date: Re: Critique my assignment please
- Previous by thread: Re: Malcolm's new book - Chapter 1 review
- Next by thread: Re: Malcolm's new book - Chapter 1 review
- Index(es):
Relevant Pages
|