Re: Question regarding fgets and new lines



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
.



Relevant Pages

  • Re: Question regarding fgets and new lines
    ... assigned storage is the callers responsibility". ... This programming style is not used by the Standard C library (and ... other well-known libraries). ...
    (comp.lang.c)
  • Re: new considered harmful
    ... >>programming style, and that abstract factory objects should be used ... one would want to provide physical separation of code into ... > libraries where this is meaningful. ...
    (comp.object)
  • Re: Question regarding fgets and new lines
    ... "The storage has been allocated within fggets ... ... storage is the callers responsibility". ... This programming style is not used by the Standard C library (and ... other well-known libraries). ...
    (comp.lang.c)
  • Re: Question regarding fgets and new lines
    ... "The storage has been allocated within fggets ... ... storage is the callers responsibility". ... This programming style is not used by the Standard C library (and ... other well-known libraries). ...
    (comp.lang.c)