Re: K&R2, exercise 4-2



arnuld <looting@xxxxxxxxxxxx> writes:

On Thu, 03 Apr 2008 21:17:30 +0530, Ben Bacarisse wrote:

arnuld <oonga@xxxxxxxxxx> writes:

if( match_num )
{
printf("Position %d is the last to match\n\n", matched_idx);
}
}

... when there is no match, the output is "left hanging". Output that
does not end with a newline is guaranteed to appear on all systems.

I really don't get it at all. If there is no match then condition will
fail and hence othing to do.

No, you are right. I based my comment on an old version that
sometimes did this:

./arnuld
a term
^D
1 matches,
0 is the last to match$

The $ is my system's prompt. Your new fixed version prints nothing if
there is no match, but it takes a while to reason that out. You
should probably remove the test "if (match_num)" since it suggests
that a partial line can be printed. The test is redundant.

For simple testing, I prefer to read a whole line, storing only those
characters that fit but this way is fine. Except...


I am at chapter 4 where authors did not start discussing the "whole line"
concept except using a rudimentary function named getline() to do
that.

I was suggesting an alternative, that is all. You do, essentially,
this:

while (there-is-room && next-char-is-not-newline)
store-that-char;

I often write:

while (next-char-is-not-newline)
if (there-is-room)
store-that-char;

so that a whole line is always read, even when there is not room for
it all.

if( c == '\n' )
{
s[i++] = '\n';
}
}
s[i] = '\0';

... I think you have a bug here. The loop above can put max-1 chars
into the buffer (max being what it was originally, of course) but it can
end when c == '\n'. You then try to put the '\n' and the '\0' in the
one remaining space!

sorry, this function is just a copy of K&R2 example, as I said earlier.
so, something wrong with K&R2 ?

I'd love to be the one to find a bug in K&R2 after all those eyes have
looked at it but, no, K&R is correct. There are two getline functions
in the book (I have K&R first edition so I won't give you page
numbers). The first tests against i < lim-1 and the second has a test
for --lim > 0 *before* reading the next character. Your code is
slightly different and has the bug I described.

The moral is that you can't just change

for (i = 0; --max > 0 && (c = getchar()) != EOF && c != '\n'; ++i)

into

for (i = 0; (c = getchar()) != EOF && c != '\n' && --max > 0; ++i)

with no consequences.

Interestingly, both of K&R's getline functions exhibit undefined
behaviour when called with a buffer size of 1. This is daft enough
not to be a bug as such, but I would have preferred to avoid it in a
teaching text.

match_num = 0;

OK, maybe you do this for testing, but a utility function like this
should not use a file-scope (AKA "global") variable like match_num. Of
course, you code works if you just make match_num local to this
function.

I have used match_num in main() function and that is why I made it global.
If it is local then main() is not going to access it.

That is fine for now, but global variables are not a good way to
communicate with general-purpose functions like str_index. When you
have got further though the book, you'll find ways to get more
information out of a function that means you don't need global
variables like that.

--
Ben.
.



Relevant Pages

  • Re: Program hangs at _dl_sysinfo_int80, what does it mean?
    ... > of my global variables which checks the number of incoming messages ... The most likely cause is a bug in your program. ... Too many levels of nested signals, ... In order to understand recursion you must first understand recursion. ...
    (comp.os.linux.development.apps)
  • Re: Are global variable evil?
    ... when a bug arises that is suspected to be ... perhaps a singleton class, ... Bugs related thereto are 'relatively' easier ... > He insists I should not have any global variables, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to submit a bug to microsoft on .NET javascript?
    ... you could install the beta of the next release, and if its still there and report a bug. ... It uses 5 variables for rendering the control: ... they are not treated as local variables, but global variables. ... When I try to use the ValidationSummary control, a javascript error is ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Global Variable passed as IN parameters, bug or feature?
    ... | I have a strange thing happening related to global variables, ... | I have this package. ... Is it a feature, or a bug? ...
    (comp.databases.oracle.server)