Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Wed, 16 Jan 2008 11:11:17 +0000
spinoza1111 <spinoza1111@xxxxxxxxx> writes:
On Jan 16, 3:41 am, Ben Bacarisse <ben.use...@xxxxxxxxx> wrote:<snip>
spinoza1111 <spinoza1...@xxxxxxxxx> writes:
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>
<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!
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.
.
- Follow-Ups:
- References:
- Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- From: spinoza1111
- Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- From: spinoza1111
- Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- From: Ant
- Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- From: Richard Heathfield
- Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- From: spinoza1111
- Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- From: Richard Heathfield
- Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- From: spinoza1111
- Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- From: Ben Bacarisse
- Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- From: spinoza1111
- Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- From: Ben Bacarisse
- Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- From: spinoza1111
- Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- Prev by Date: Re: Bare-Metal Programming
- Next by Date: Re: Herbert Schildt, author of The Complete C++ Reference (NOT C Unleashed) rehabilitated on wikipedia
- Previous by thread: Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- Next by thread: Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
- Index(es):
Relevant Pages
|