Re: good algorithms come with practice and reading good code/books?



newsman654@xxxxxxxxxxxxx said:

Richard Heathfield <rjh@xxxxxxxxxxxxxxx> wrote:
newsman654@xxxxxxxxxxxxx said:

<snip>

...which tells us all we need to know about the quality of your advice.


All I really use that book for is a reference to the c library functions.

Nice spot of back-pedalling. :-)

Unfortunately, it's no good as a library reference either.

For example, it suggests in the gets() description that "it is your job to
make sure that the array pointed to by str is not overrun", hinting that
this job is actually possible (which it isn't). This wouldn't be so bad if
Schildt warned against using gets(), but of course he uses it freely
throughout the book.

For example, it claims in the getchar() description that "since EOF is a
valid integer value, you must use feof() to check for end-of-file when
working with binary files", which is complete nonsense.

For example, it claims that fwrite() and fread() return int, whereas in fact
they return size_t.

For example, it claims that fflush() clears the contents of the input buffer
if given an input stream's file pointer, whereas in fact the behaviour is
undefined.

For example, it suggests in the strncpy() description that "if the string
pointed to by str2 is longer than count characters, the resultant string
pointed to by str1 is not null terminated", whereas in fact this happens if
the source string is longer than or equal to count characters.

In the same description, it gives the following example:

---- begin quote ----
The following fragment copies at most 79 characters of str1 into str2,
thus ensuring that no array boundary overflow occurs.

char str1[128], str2[80];

gets(str1);
strncpy(str2, str1, 79);
---- end quote ----

In fact, it ensures no such thing, for two reasons. Firstly, the use of
gets() means that it cannot be ensured that no array boundary overflow has
occurred. Secondly, since str2's value is initially indeterminate, if the
input given to gets() is exactly 79 characters long (not including the
terminator), those 79 characters will be copied, but str2 will not be
null-terminated, and thus even a printf is likely to violate the bounds of
the array (I say "likely" because it's just possible that str2[79] will
happen to be a '\0'). Thus, array bounds overflow prevention is *not*
ensured.

For example... well, I found those half-dozen obviously flawed examples in
the first six functions I looked at. Heaven knows how many more such
obvious errors there are, and we haven't even started to consider the
*subtle* errors.

But you are correct, Herb Schildt's book should not be used for much else.

s/for much else/except as a door-stop/

Let me correct myself and suggest to pick out a good reference to the
functions in the C library.

"The C Programming Language", 2nd edition, by Kernighan and Ritchie, is an
excellent reference.

I would suggest to exhaust what exists in the libraries and 'know' the
functions, so the beginner isn't reworking what's already there.

That, at least, is sound advice. :-)

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
.



Relevant Pages

  • Re: <ctype.h> toLower()
    ... > HTH ... characters and better to check before calling the function. ... by reference rather than returning a pointer, unless you need to return a ... it's just called passing by reference. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: problem with function
    ... Yes I did miss type the psz example, it is a pointer to char (not pointer to ... companies using Hungarian, and when I looked into one standard, I found ... >> If you intended copying the string, you need to use the strcpy function. ... > Isn't 'psz' a pointer a an array of characters containing a string? ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Right justifying (arbitrary) alphanumeric input
    ... I have since learned that expressions are allowed in reference ... > modificaion, once I separate the operators from the operands. ... without a space - those 'semi-words' get stored with differing lengths ... keeping a count of total characters found ...
    (comp.lang.cobol)
  • Re: Separating directory from file name: Code Review!
    ... > return a null pointer in this example, ... > handed a string that does not contain a '/' character. ... > memory for an array of characters and have it all zeroed, ... > calloc() will do both operations in one call. ...
    (comp.lang.c)
  • Re: ctypes issues involving a pointer to an array of strings
    ... The digits in that pointer value look suspiciously like the ... It looks like the first 4 characters of a string starting ... make an array of just one item, ... of devices returned by the deviceCount parameter. ...
    (comp.lang.python)