# Segfault by running function twice

Jeremy Cowgar jeremy at cowgar.com
Fri Aug 22 21:59:55 UTC 2008

```I have created a function to compute the Levenshtein distance between
two strings, however, if I run it twice in the same application I get a
seg fault. You can call it on any combination of strings and it works,
but call it twice and you get a Segfault on the 2nd call of the
function. Can anyone look at this? I am not seeing an obvious problem in
the function but I certainly could be overlooking something.

Thanks! Jeremy

--------

+ name := LEVENSHTEIN;

Section Private

- levenshtein (s,t:ABSTRACT_STRING) :INTEGER <-
// Compute the difference (edit distance) between two strings. The
distance
// is the minimum number of edits necessary to transform `s` into
`t`. For
// instance: Cat to Hat requires changing C to H, 1 edit. Dog to Mop
// requires D to change to M, and g to change to p, 2 edits. Saturday to
// Sunday delete "at" in Saturday (2 edits) and change the r to n
and you
// have Sunday, 3 edits.
(
+ n, m, a, b, c, cost : INTEGER;
+ d : FAST_ARRAY2[INTEGER];

n := t.count + 1;
m := s.count + 1;

d := FAST_ARRAY2[INTEGER].create (m, n);

0.to m do { i:INTEGER;
d.put i to (i,0);
};

0.to n do { j:INTEGER;
d.put j to (0,j);
};

1.to m do { i:INTEGER;
1.to n do { j:INTEGER;
( (s.item i) = (t.item j)).if { cost := 0 } else { cost
:= 1 };
a := d.item (i-1, j) + 1;
b := d.item (i, j-1) + 1;
c := d.item (i-1, j-1) + cost;

d.put ((a.min b).min c) to (i,j);
};
};

d.item (m,n)
);

- say_distance (a,b:ABSTRACT_STRING) <-
(
("Distance between " + a + " and " + b + " is: " +
levenshtein(a,b).to_string + "\n").print;
);

Section Public

- main <-
(
//say_distance("Dog", "Mop"); // 2
//say_distance("Cat", "Hat"); // 1
say_distance("Saturday", "Sunday");  // 3
);

```