Re: Malcolm's new book - Chapter 1 review



Keith Thompson wrote:
CBFalconer <cbfalconer@xxxxxxxxx> writes:
Flash Gordon wrote:
[...]
Keith's point is that if the user of the library function could
specify a maximum size (possibly 0 meaning unlimited) then the
user of the library function could decide on some suitable upper
bound.

I wrote ggets{} to replace gets{}. It maintains the simplicity -
you supply only the address of a pointer, which will receive the
pointer to the next input line. The only other thing to worry
about is the return value, which can be 0 (good), EOF (EOF) or
positive non-zero (I/O error). Now you have to remember to arrange
to free() that pointer at some time. You can also copy it
elsewhere, embed it in a linked list, etc. etc.

However, use is always totally safe. The input action will never
overwrite anything. If you put any limits on it, sooner or later
those will bite. Or they are one more parameter to "get right"
before calling. The simplest parameter is no parameter. It is
fairly hard to get that one wrong.
[...]

A program using ggets(), reading from an arbitrary input file, can
attempt to allocate an arbitrarily large amount of memory. It will
eventually fail cleanly (assuming malloc and realloc work the way
they're required to), but even so, allocating as much memory as you
can may have negative consequences. I can write a program that calls
malloc() in a loop to see how much I can allocate, but I wouldn't want
to run it on a shared system.

I'm not suggesting changing the default behavior, just providing a way
for the user to change it. You could either provide a routine to set
a maximum size for future calls (though that could introduce issues
for threaded environments), or provide an additional function that
lets you specify a limit. (The behavior on exceeding the limit would
have to be defined.)

Heck, since it's public domain, I might go ahead and make some changes
myself. Naturally you're under no obligation to accept them; and if I
distribute it myself I'll certainly give you credit. I'll do this in
my copious free time, of course, so don't hold your breath.

Since it is PD you can do whatever you wish. However I request
that, if you change the header file in any way, you also change the
routines name.

Note the simplicity and safety of the demo file reverse.c.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>



--
Posted via a free Usenet account from http://www.teranews.com

.



Relevant Pages

  • Re: Malcolms new book - Chapter 1 review
    ... user of the library function could decide on some suitable upper ... pointer to the next input line. ... attempt to allocate an arbitrarily large amount of memory. ... lets you specify a limit. ...
    (comp.lang.c)
  • Re: basic_string ctor
    ... > Which doesn't make it right - C strings are lame at best and we all know ... They are not lame - they just have their drawbacks. ... > pointer is entirely up to the object's constructor to decide. ... Now the internal library function was like ...
    (microsoft.public.vc.stl)
  • Re: Writing bulletproof code
    ... That wild pointer might just point to ... > on and ship with debug off. ... Exceptions allow graceful management of errors. ...
    (comp.programming)
  • Re: HardBound and SoftBound
    ... technologies such as HardBound and SoftBound enable, in their tightest checking modes, for programming languages such as C and C++. ... (This somewhat diffident phraseology is intended to emphasize that technologies such as HardBound and SoftBound do not specify the programming language semantics; they just implement a semantics that the researchers believe is either compatible with the official semantics, or compatible with what the vast majority of users expect. ... In general, pointer extents are associated with pointer expressions, as well as with pointer variables and constants and parameters. ...
    (comp.arch)
  • Type Lib editor from Curland
    ... Still looking into making a Windows TypeLib and it looks the simplest way is to use the Type Library editor from ... Specify a handle opened by the WTSOpenServer function, or specify WTS_CURRENT_SERVER_HANDLE to indicate the terminal server on which your application is running. ... Pointer to a variable that receives a pointer to an array of WTS_PROCESS_INFO structures. ... Pointer to a variable that receives the number of WTS_PROCESS_INFO structures returned in the ppProcessInfo buffer. ...
    (microsoft.public.vb.general.discussion)