Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia



spinoza1111 <spinoza1111@xxxxxxxxx> writes:

On Jan 16, 3:41 am, Ben Bacarisse <ben.use...@xxxxxxxxx> wrote:
spinoza1111 <spinoza1...@xxxxxxxxx> writes:
<snip>
You left it to me to find the problem. I
ran the code, and .Net's runtime showed me the offending statement. I
almost threw up when I thought that Kernighan had published a code
that deliberately went one beyond the end of the string,

The published code is correct.

I haven't found a single "bug" in it. I have found many errors,
starting with the error of claiming that it processes strings when all
it processes are arrays of bytes and continuing with its use of
idiomatic C to claim to teach anything at all about computer science.
Within its framework, which sucks, it is a correct program. Which
sucks insofar as to suck is hereditary.

You would enjoy it more if you saw it in terms of what it set out to
be not what you expected it to be. And I think you are placing too
low a value on correctness.

<snip>
Please explain how my efforts to improve the code caused the second
error case

You wanted to improve the code by reporting where the match occurs.
To do this you changed a value parameter to a reference parameter and
this makes the recursion go wrong.  It "goes wrong" differently with
the C++ and C# version.

Thanks!
<snip>
I understand that if I pass the address for reference call, it's going
to always update the first parameter at the top of recursion UNLESS it
copies the address into a work area. But how is that a problem?

Because it alters the algorithm. Your code is not a transliteration
of Pike's original into C++ and C# with additional features, it is a
different program with different (and now incorrect) semantics. Take
one of the test cases I gave you and trace what the original does and
what your code does and I think you will find that they do not match
the same things.

The recursive algorithm relies on the parameters to remember where
things matched, and sharing one variable all the way down the call
sequence breaks it.

The C++ and C# versions behave differently to each other because you
chose to share different things (an index in C# and a pointer to the
rest of the string to match in C++) but I think they both go wrong for
the same fundamental reason based on the same attempt to improve the
code.

Can you explain how C++ and C differ in the handling of reference
parameters passed as &a?

I take it you don't mean "in general", because if you do I can't help
because I don't know C#. If you are asking why your two version
behave differently, it is because they share different things using a
"reference" (see the previous paragraph).

You choose for some reason (presumably lack of familiarity) *not* to
use real reference parameters in C++, but instead you used another
level of indirection (another * in the type) as one would have to in
C. Using C++ references (i.e. char *&) would have made your intent a
lot clearer in my opinion.

It may seem a small point to you, but I am still not sure your code is
really C++. The reference syntax for C++ that I use does not have
"value" as a keyword and I had to comment out:

/* public value */ class kernighanRegexC

to get g++ to compile it. As a result, all my comments are rather
conditional. If your code in not C++, then there may be subtle
differences elsewhere between what I think C++ means, and what you
intended in the similar but related language.

--
Ben.
.



Relevant Pages

  • Re: Recursive functions
    ... recursion is certainly a natural way of defining the ... it IS totally inappropriate to compute ... not mean "inefficient in a typical C implementation". ... If I had an actual requirement to print the string ...
    (comp.lang.c)
  • Re: Three Kinds of Logical Trees
    ... > I think this is just a representation issue. ... > I do have a number, but I am only ttreating as a string. ... I was attempting to isolate the question of tree structure; ... it's not much of an issue; no one uses recursion much. ...
    (comp.databases.theory)
  • Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
    ... a string of "any" character can be terminated by dot asterisk. ... lacking the knowledge and professionalism to explain the errors to ... (Adding "the error Ben merely found by experimentation" does seem to ... Within its framework, which sucks, it is a correct program. ...
    (comp.programming)
  • Is this recursion?
    ... This is the ole reverse a string classroom assignment. ... Recursion is soemthing I never quite understood till now but as I ... public static void reversal{ ...
    (comp.lang.java.help)
  • Re: Recursive functions
    ... function to compute the length of a string. ... to define the length of a finite sequence in terms of recursion ... :) I've never taught any programming courses, ...
    (comp.lang.c)