Re: Question regarding fgets and new lines
- From: Eric Sosman <esosman@xxxxxxxxxxxxxxxxxxx>
- Date: Fri, 24 Nov 2006 17:48:59 -0500
CBFalconer wrote:
Roland Pibinger wrote:
On Fri, 24 Nov 2006 02:22:25 -0500, CBFalconer wrote:
Or simply download and use the public domain ggets, at:
<http://cbfalconer.home.att.net/download/>
"The storage has been allocated within fggets ... Freeing of
assigned storage is the callers responsibility".
This programming style is not used by the Standard C library (and
other well-known libraries). I'd be reluctant to use it in my
programs.
Why not? If you malloc something, you know you need to free it
when no longer needed. If you use ggets, you know you need to free
the line when no longer needed. This is not a massive memory
leap. Meanwhile you don't have to worry about buffer sizes, etc.
FWIW, I took a somewhat different tack in my own gets()
replacement (I guess everybody writes one, sooner or later).
Mine follows the precedent of things like getenv(): the returned
pointer is only valid until the next call, when the buffer it
points to may be overwritten and/or moved or freed.
This approach has some disadvantages: for example, it would
be a pain to make it thread-friendly. On the other hand, it
localizes all the memory management inside the function, and the
signature `char *getline(FILE*)' is simple enough that even I can
remember it. (The older and feebler my gray cells get, the more
I value simplicity ...)
I don't have a convenient place to post the code, but I'll be
happy to mail it to anyone who's interested.
--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxx
.
- Follow-Ups:
- Re: Question regarding fgets and new lines
- From: Richard Heathfield
- Re: Question regarding fgets and new lines
- From: CBFalconer
- Re: Question regarding fgets and new lines
- From: Richard Heathfield
- Re: Question regarding fgets and new lines
- References:
- Question regarding fgets and new lines
- From: mellyshum123
- Re: Question regarding fgets and new lines
- From: Eric Sosman
- Re: Question regarding fgets and new lines
- From: CBFalconer
- Re: Question regarding fgets and new lines
- From: Roland Pibinger
- Re: Question regarding fgets and new lines
- From: CBFalconer
- Question regarding fgets and new lines
- Prev by Date: Re: screen clearing in ANSI C
- Next by Date: Re: scanf behaviour
- Previous by thread: Re: Question regarding fgets and new lines
- Next by thread: Re: Question regarding fgets and new lines
- Index(es):
Relevant Pages
|
|