Reputation: 285
I'm trying to make a c/c++ program where the program accepts a txt file with several words, one per line, and finds the edit distance(also called levenshtein distance) with a specific word.
I have a weird problem.
My code encounters a runtime error after a reading a couple of words when I run it in code-blocks. It debugs fine when i use the code-blocks debugger.
I have been looking around and I found uninitialised variables can be a problem. But whenever i comment the line where i am calling the function minDistance
count[i]=minDistance(word,lines[i]);
, the code runs fine and prints out all the words in the file. So that's not a problem I guess.
Any help would be great. Thank you.
Following is the code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<iostream>
using namespace std;
static int minDistance(char* word1, char* word2)
{
const int l1 = strlen(word1);
const int l2 = strlen(word2);
int i=0,j=0;
int **d = new int*[l2 + 1];
for(i=0;i<l1+1;++i)
d[i]=new int[l1+1];
// the edit distance between an empty string and the prefixes of
// word2
for (i = 0; i < l2 + 1; i++) {
d[0][i] = i;
}
// the edit distance between an empty string and the prefixes of
// word1
for (j = 0; j < l1 + 1; j++) {
d[j][0] = j;
}
for (i = 1; i < l1 + 1; i++) {
for (j = 1; j < l2 + 1; j++) {
if (word1[i - 1] == word2[j - 1]) {
d[i][j] = d[i - 1][j - 1];
} else {
d[i][j] = min(min(1 + d[i][j - 1], 1 + d[i - 1][j]),
1 + d[i - 1][j - 1]); // min of insertion,
// deletion, replacement
}
}
}
return d[l1][l2];
}
void lines()
{
int i=0;
char * lines[10];
int count[10];
char word[]="book";
FILE *file_handle = fopen ("wordlist.txt", "r");
for (i =0; i < 5; ++i)
{
lines[i] = (char*)malloc (128); /* allocating a memory slot of 128 chars */
fscanf (file_handle, "%s", lines[i]);
count[i]=minDistance(word,lines[i]);
cout<<lines[i]<<" ";
cout<<count[i]<<endl;
}
for (i =0; i < 5; ++i)
free (lines[i]);
}
int main (int argc, char *argv[])
{
lines();
return 0;
}
Upvotes: 1
Views: 87
Reputation: 119
You use l2 as your second index. It should be your first index and l1 your second index.
// the edit distance between an empty string and the prefixes of
// word2
for (i = 0; i < l1 + 1; i++) {
d[0][i] = i;
}
Upvotes: 0
Reputation: 3295
Notice the lines in your code :
int **d = new int*[l2 + 1];
for(i=0;i<l1+1;++i)
You are allocating memory for (l2 + 1)
number of int*
and you are looping i
from 0 to (l1 + 1)
. So if l2 < l1
, you are accessing memory you have not allocated.
Also don't mix C++ and C. Either use C or stick with C++. As mentioned in the comments, if you can use C++, use std::vector
and std::string
- it will reduce your headache. Also use C++'s IO classes to perform File IO and always close any file you have opened. (i.e. in C, use fclose(file_ptr)
).
Upvotes: 2