I have two strings s1: "abcd"
and s2: "ab"
and two int vectors v1: 12,13,14,15
and v2: 12,13
. I am calculating the distance between the two strings and the two vectors. I have got the code from the web for a c
file to calculate the distance between the two strings. I am modifying the code in a way to calculate the distance between two vectors, but in c++
. The code is working, but the result are not the same (the distance should be the same for both calculations). The problem comes from:
double transpositions = 0.0;
for (i = 0; i < s1_len; i++) {
if (!s1_matches[i]) continue;
while (!s2_matches[k]) k++;
if (s1[i] == s2[k]) transpositions++;
k++;
}
The s1_len
is s1_len(s1)
(the same for s2) and the s1_match
is int *s1_matches = (int*) calloc(s1_len, sizeof(int))
. I am running the c
code with correct answers, but when I change the code to c++
and change the string variables to vector variables, the number that I get for transposition
is different. To change the string to vector, I get the size of the vector ( v1_len
) by v1.size()
and the v1_match
by int *v1_matches = (int*) calloc(v1_len, sizeof(int))
. The code is exactly the same:
double transpositions = 0.0;
for (i = 0; i < v1_len; i++) {
if (!v1_matches[i]) continue;
while (!v2_matches[k]) k++;
if (v1[i] == v2[k]) transpositions++;
k++;
}
What is the problem here?
You do not seem to do any bounds checking on s2_matches
or v2_matches
. This will have undefined behaviour when s1
and s2
are not identical:
while (!s2_matches[k]) k++;
if (s1[i] == s2[k]) transpositions++;
k++;
ie You never check for k
overflowing the bounds of s2
or s2_matches
. Same with the vector version.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.