Re: Variable-sized lines of text in linked list



CBFalconer said:

Richard Heathfield wrote:
CBFalconer said:
Scottman wrote:

I am trying to read a text file into memory without any knowledge
of how long each line will be. I am looking to store each line
in a linked list structure, however, I am unsure of how to
dynamically allocate space for each line.

Just get the (short and simple) function ggets(), in ggets.zip.

...and be aware of its problems. Chuck, if you're going to keep on
pimping this function, shouldn't you at least warn people of its
shortcomings?

I don't consider them shortcomings. They are that, since the
routine can collect lines of ANY length, that if you can supply a
sufficiently long line without any line terminations (normally
meaning <return> chars typed) the routine will malloc sufficient
space. If you then fail to free that space (normally a memory leak
error) it won't be freed, and you can run out of memory. Unlikely
in practice, and requires careful programmer and user
concentration.

In fact, there are several problems.

The most obvious one, the problem of an immensely long line that might
exhaust storage, is simply a consequence of the fact that computers don't
have infinite storage, and all such functions suffer from this problem, so
it is not specific to ggets - I was not including it in ggets's
shortcomings.

The next problem is one that you refer to yourself - the risk of a memory
leak. Again, any function that seeks to acquire arbitrarily long lines
will run this risk, but ggets exacerbates the problem by requiring the
caller to free the buffer after every single call. This is a problem that
can be overcome by allowing the caller to provide a buffer, the length of
its current contents, and perhaps its current maximum capacity, so that
the function can try to acquire memory only when it needs to, and the
caller need not be burdened with constant calls to free().

A third problem is that of Denial of Memory attacks, where malicious users
seek (perhaps over a remote connection) to bring the program down by
asking it to deal with ludicrously long lines. One obvious way to deal
with this is to give the function information on the largest line with
which it can reasonably be expected to cope, and to allow it to reject or
perhaps truncate longer lines.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
.



Relevant Pages

  • Re: Variable-sized lines of text in linked list
    ... I am trying to read a text file into memory without any knowledge ... Just get the function ggets(), ... I don't consider them shortcomings. ... caller to free the buffer after every single call. ...
    (comp.lang.c)
  • Re: Garmin GPS V Deluxe still a good buy?
    ... its shortcomings are: ... - small memory, make sure its big enough for you ... Matt ... Prev by Date: ...
    (sci.geo.satellite-nav)
  • Re: return the start of a substring in a string in c
    ... Why not the explanation of how strstrand its caller ... much faster than the memory can deliver and absorb it. ... a CPU running at "100% utilization" is often ... LP vinyl records onto CD via my home computer's sound card. ...
    (comp.lang.c)
  • Re: style question,itoa
    ... sensible to have the caller pass in a buffer. ... technically useless malloc and free calls, ... priori when memory allocations would fail. ...
    (comp.unix.programmer)
  • Re: [PATCH 1/7] async: Asynchronous function calls to speed up kernel boot
    ... - solves the case of the internal implementation getting a failed ... lets the infrastructure to do the allocation ... We could provide a small wrapper that allocates memory for the simple ... case - but having the caller provide the memory makes it more flexible. ...
    (Linux-Kernel)