Write to file

B

Ben Bacarisse

CBFalconer said:
Try this. It's somewhat simpler (and faster and bufferless).

/* And heres another throw -- binfsrch.c by CBF */
<Knuth-Morris-Pratt implementation snipped>

Only three problems with this:

(1) The OP is starting out in C. Do you think an implementation of
KMP is simpler than the one-char at a time match I suggested they try
to implement?

(2) It does not do what they said they were trying to do. They wanted
a search and replace and with your version of KMP it is tricky to
convert to a version that echos up to the start of a match. Not that
hard for us, but were you suggesting a beginner should try to do it?

(3) It is wrong -- specifically it invokes the dreaded UB by writing
outside the bounds of an array.
 
B

Ben Bacarisse

CBFalconer said:
Oh? Where? I believe you are mistaken.

while (i < lgh) {
while ((j >= 0) && (id != id[j])) j = next[j];
i++; j++;
next = j; /* <== Here. 'next' is of size 'lgh'. */
}

The program writes to only one array and in only one place so there are
not may places to check!
 
C

CBFalconer

Ben said:
CBFalconer said:
Oh? Where? I believe you are mistaken.

while (i < lgh) {
while ((j >= 0) && (id != id[j])) j = next[j];
i++; j++;
next = j; /* <== Here. 'next' is of size 'lgh'. */
}

The program writes to only one array and in only one place so
there are not may places to check!


Bah humbug. Looks like I goofed, and the fact that my malloc acts
on the ((length + 15) modulo 16) hid it. I just made the malloc
allocate 1 more byte. Thanks for the correction.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,600
Members
45,179
Latest member
pkhumanis73
Top