Re: Variable-sized lines of text in linked list
- From: Richard Heathfield <rjh@xxxxxxxxxxxxxxx>
- Date: Fri, 29 Feb 2008 13:01:21 +0000
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
.
- Follow-Ups:
- Re: Variable-sized lines of text in linked list
- From: CBFalconer
- Re: Variable-sized lines of text in linked list
- From: Richard Tobin
- Re: Variable-sized lines of text in linked list
- References:
- Variable-sized lines of text in linked list
- From: Scottman
- Re: Variable-sized lines of text in linked list
- From: CBFalconer
- Re: Variable-sized lines of text in linked list
- From: Richard Heathfield
- Re: Variable-sized lines of text in linked list
- From: CBFalconer
- Variable-sized lines of text in linked list
- Prev by Date: Re: Is there stack associated when a executing an inline function?
- Next by Date: Re: Variable-sized lines of text in linked list
- Previous by thread: Re: Variable-sized lines of text in linked list
- Next by thread: Re: Variable-sized lines of text in linked list
- Index(es):
Relevant Pages
|