Dynamic 2d Array Memory Leak Problem/Question

I am writing a program to compute the levenshtein distance between two strings based on two command line arguments(text files), and this currently does it. However, when I compile in visual studio sometimes it will work and print out the program, but most of the time I get a breakpoint error regarding the heap.
I was wondering what I should do to my matrix in order to fix the what I assume to be memory leaks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
  #define NOMINMAX
#define min(a,b)            (((a) < (b)) ? (a) : (b)) 
#include <iostream>
#include<string>
#include<fstream>
 
 
using namespace std;
 
 
int main(int argv, char ** argc)
{
	string sStore;
	string tStore;
	int sLen = 0;
	int tLen = 0;
 
	if (argv >= 3)
	{
		cout << argc[1] << endl;
		cout << argc[2] << endl;
 
		ifstream s;
		s.open(argc[1]);
 
		ifstream t;
		t.open(argc[2]);
 
		if (s.fail() || t.fail())
		{
			cout << "One of the files failed to open " << endl;
		}
 
		s >> sStore;
		cout << sStore << endl;	
		t >> tStore;
		cout << tStore << endl;
 
		sLen = strlen(sStore.c_str())+ 1;
		tLen = strlen(tStore.c_str())+ 1 ;
 
		int** matrix = new int*[sLen]; // created the matrix here
		for (int i = 0; i < sLen; i++)
		{
			matrix[i] = new int[tLen];
		}
 
 
		for (int i = 0; i < tLen; i++)
		{
			matrix[i][0] = i;
		}
		for (int i = 0; i < sLen; i++)
		{
			matrix[0][i] = i;
		}
		//These two fill the first row & column of the matrix
 
 
		for (int i = 1; i < tLen; i++)
		{
			for (int j = 1; j < sLen; j++)
			{
				if (sStore[j - 1] == tStore[i - 1])
				{
					matrix[i][j] = matrix[i - 1][j - 1];
					//Aux matrix = 0;
				}
				else
				{
					matrix[i][j] = min(min((matrix[i - 1][j] + 1), (matrix[i][j - 1] + 1)), (matrix[i - 1][j - 1] + 1));
				}
			}
		}
		for (int i = 0; i < tLen; i++) //printing the matrix.
		{
			for (int j = 0; j < sLen; j++)
			{
				//matrix[i][j] = 0;
				cout << matrix[i][j] << ' ';
			}
			cout << "\n";
		}
 
		s.close();
		t.close();
 
		for (int i = 0; i < tLen; i++)
		{
			delete[] matrix[i];
		}
		delete[] matrix;
	}
 
 
	system("pause");
}
Apart from anything else you appear to be (repeatedly) confusing row and column length.

Try changing slen on lines 42 and 43 to tlen
and changing tlen on line 45 to slen

Then first index i will range from 0 to tlen-1
and second index (usually j, apart from lines 53-56) will range from 0 to slen-1

I think the rest of the program will then at least be consistent.

I'm not sure what you are aiming to do. All that your program will do is read the first word from each of two text files. You could simply supply these words on the command line.

You are also missing two headers, <cstring> (for strlen) and <cstdlib> (for system).
Last edited on
Hi,

I made the changes and my program is the same but the memory leak is still there; The point of the program is for the user to type in two text files and then I store the strings and compute the edit distance to make the two strings identical; Some text files are more then one line (like human RNA sequence vs cow RNA sequence). I still have to make the program store strings longer then one line, I was going to do that after.

For example, for my current text files (s1 containing 'saturday' and t1 containing 'sunday') This is the output I get sometimes when I run the program(Which is correct)




S a t u r d a y
=============================
| 0 1 2 3 4 5 6 7 8
S | 1 0 1 2 3 4 5 6 7
u | 2 1 1 2 2 3 4 5 6
n | 3 2 2 2 3 3 4 5 6
d | 4 3 3 3 3 4 3 4 5
a | 5 4 3 4 4 4 4 3 4
y | 6 5 4 4 5 5 5 4 3


But the program triggers a breakpoint most other times and it does not print out this part of the program when the breakpoint is triggered and I was wondering if it was because of memory allocation with the matrix(which I can't seem to figure out how to fix).


Thank you again!
Are you saying that, with the same input, your program sometimes works and sometimes doesn't? Very odd. Try hard-coding your strings rather than reading from file.

Also, there is no need to mix strings and c-strings - just use the .size() member function to get the relevant lengths.

Why do you think it is a "memory leak"? Out-of-bounds array access, perhaps, but that's not the same thing. You could also use vectors of vectors instead of multi-dimensional dynamic arrays. There are then means of checking array bounds on access.



Well, as suggested in my earlier reply I changed the following:
- changed slen on lines 42 and 43 to tlen;
- changed tlen on line 45 to slen;
- added header <cstring>;
- commented out system("pause") - it's a bit irrelevant from the command line;

I put "Saturday" (without quotes) in s1, "Sunday" (without quotes) in s2 and ran my executable temp.exe as:
temp s1 s2


This gives the following output ... every time ... as
s1
s2
Saturday
Sunday
0 1 2 3 4 5 6 7 8 
1 0 1 2 3 4 5 6 7 
2 1 1 2 2 3 4 5 6 
3 2 2 2 3 3 4 5 6 
4 3 3 3 3 4 3 4 5 
5 4 3 4 4 4 4 3 4 
6 5 4 4 5 5 5 4 3


So I can't explain what you are having problems with.

BTW, a "breakpoint" is something you set and I suspect you meant something else. The same with "memory leak".

Also, if you are going to create a matrix to carry out this analysis, then your 1-d strings (human RNA!) had better not be too long.
Last edited on
So the breakpoint error I got in visual studio disappeared when I changed lines 43/42 and 45, The code works now. Thank you so much!
Topic archived. No new replies allowed.