Re: Variable-sized lines of text in linked list
- From: CBFalconer <cbfalconer@xxxxxxxxx>
- Date: Fri, 29 Feb 2008 10:09:41 -0500
Richard Heathfield wrote:
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>
However you carefully snipped the portion where I pointed out that
this is no different than using the malloc family in any program.
--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com
.
- Follow-Ups:
- Re: Variable-sized lines of text in linked list
- From: Richard Heathfield
- 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
- Re: Variable-sized lines of text in linked list
- From: Richard Heathfield
- Variable-sized lines of text in linked list
- Prev by Date: Re: Variable-sized lines of text in linked list
- Next by Date: dual core and c
- 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
|