bad_alloc due to memory allocation in a loop

Hey all.
I am brand new to C++, but decided to try it out for a project I am working on.
The program I am trying to write takes two input files, each of which contains a few different lists of chars. File1 (mouse.fa) has 4 lists with an average of 130 chars in each. File2 has 186 very long lists, averaging 19-20K chars per list. seq1 and seq2 are vectors of vectors that hold all of the sequences from File1 and File2.

When I run the code I get an instance of bad_alloc from one of the loops that allocates memory (see the comment with the giant arrow). This always happens when i'm iterating through the second element of seq1 and the 21st element of seq2(but the length of both of these elements is normal). The weird thing is that "delete tbtt" is one of the final commands at the end of the larger loop that this statement is in, so as far as I can tell memory should be freed up for this operation each time...but I must be memory leaking somewhere, I'm just not sure where or how to fix it.
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160

int main(){
int gene;
vector<vector<char> > seq1;
vector<vector<char> > seq2;
vector<char> sub; 
string read;

ifstream File1;
ifstream File2;

File1.open("mouse.fa");

if(!File1.is_open()){
	cout << "Could not open the first FASTA" << endl;
	cout << "Program terminating.\n";
	exit(EXIT_FAILURE);
}
gene=-1;
while (!File1.eof()){
	std::getline(File1,read,'\n');
	if(read.length()>0){	
		if(read.at(0)=='>'){			
			gene++;
			seq1.push_back(sub);
			}
		else {			
			for(int i=0;i<read.length();i++){	
				seq1.at(gene).push_back(read.at(i));
		
			}	
		}
	}
}
File1.close();

File2.open("human.fa");
gene=-1;
while (!File2.eof()){
	std::getline(File2,read,'\n');
	if(read.length()>0){
		if(read.at(0)=='>'){			
			gene++;
			seq2.push_back(sub);
			}
		else {			
			for(int i=0;i<read.length();i++){	
				seq2.at(gene).push_back(read.at(i));
		
			}	
		}
	}
}
File2.close();

char choice;
int m,mm,g,go,ge;
char again = 'y';
int highscore = -40000;

int **bbtt;
int s1best,s2best;
int c, d;
int **dpt;
int **tbtt;

while (again == 'y'){
cout << "What type of alignment would you like to perform? Enter g for global, l for local, f for fitting, a for global with affine gap penalties, or q to quit:" << endl;
cin >> choice;
switch (choice){
case 'g': //global
	cout << "Enter the desired Match score:\n";
	cin >> m;		
	cout << "Enter the desired Mismatch penalty:\n";
	cin >> mm;
	cout << "Enter the desired Gap penalty:\n";
	cin >> g;
	for(int i=0;i<seq1.size()-1;i++){  //size-1?
		cout << "now for seq1 item # " << i; //size -1?
		for(int j=0;j<seq2.size()-1;j++){
			cout << " and for seq2 item # " << j << endl;
			//create dynamic programming table as dynamically allocated pointer
			dpt = new int*[seq1[i].size()+1];			
			for(int k = 0;k<=seq1[i].size();k++){
				dpt[k]=new int[seq2[j].size()+1];
			}
			//create temp backtrace table
			tbtt = new int*[seq1[i].size()+1];			
			for(int k = 0;k<=seq1[i].size();k++){
   //==============>>>>>>>>        the allocation below throws the error
				tbtt[k]=new int[seq2[j].size()+1];
			}
			//intialize table
			dpt[0][0]=0;
			tbtt[0][0]=0; //0 terminates backtracking
			for(int k=1;k<seq1[i].size()+1;k++){
				dpt[k][0]=dpt[k-1][0]-g;
				tbtt[k][0]=2; //down
				}
			for(int k=1;k<seq2[j].size()+1;k++){
				dpt[0][k]=dpt[0][k-1]-g;
				tbtt[0][k]=3; //left
			}
			//now fill in table
			for(int b=1;b<seq2[j].size()+1;b++){
				for(int a=1;a<seq1[i].size()+1;a++){
					if(seq1[i][a-1]==seq2[j][b-1]){	//match
						dpt[a][b]=dpt[a-1][b-1]+m;
						tbtt[a][b]=1; //diagonal in btt
						//cout << "GOT A 1 at" << a << " " << b <<endl;
					}
					else if (seq1[i][a-1]!=seq2[j][b-1]){ //mismatch
						dpt[a][b]=dpt[a-1][b-1]-mm;
						tbtt[a][b]=1; //diagonal
						//cout << "GOT A 1 at" << a << " " << b <<endl;

					}		
					if(dpt[a-1][b]-g > dpt[a][b]){ //indel
						dpt[a][b]=dpt[a-1][b]-g;
						tbtt[a][b]=2; //down
						//cout << "GOT A 2 at" << a << " " << b <<endl;
					}
					if(dpt[a][b-1]-g > dpt[a][b]){ //indel
						dpt[a][b]=dpt[a][b-1]-g;
						tbtt[a][b]=3; //left
						//cout << "GOT A 3 at" << a << " " << b <<endl;
					}
				}
			}
			
			//check if score for current alignment is highest		
			if(dpt[seq1[i].size()][seq2[j].size()]>highscore){
				cout << "new high score at " << i << " " << j <<endl;				
				highscore=dpt[seq1[i].size()][seq2[j].size()];
				s1best=i;
				s2best=j;
				//set up size of best backtrace table	
				bbtt = new int*[seq1[i].size()+1];			
				for(int k = 0;k<=seq1[i].size();k++){
					bbtt[k]=new int[seq2[j].size()+1];
					}
				//fill best backtrace table with elements from temp table
				for(int a=0;a<=seq1[i].size();a++){
					for(int b=0;b<=seq2[j].size();b++){
						bbtt[a][b]=tbtt[a][b];		
					}	
				}
				c = seq1[i].size();
				d = seq2[j].size();			
	
			}
			delete dpt;
			delete tbtt;
		}
	}
	Print(c,d,bbtt,&seq1[s1best],&seq2[s2best]);
	cout << endl;
	cout << "The score of the optimal global alignment shown above is "<<highscore<<endl; 
	delete bbtt;
	break;


Sorry if it looks a bit ugly in the code tags--some of the lines spill over because of the size of this text window. Thanks in advance for any help you have to offer.

-J
I believe I solved the error--it turns out that every subarray of dpt and tbtt must be deleted individually, so i had to add these statements to properly clean up the allocated memory
1
2
3
4
5
6
7
8
	for(int a=0;a<seq1[i].size()+1;a++){
					delete [] dpt[a];
			}			
			delete [] dpt;
			for(int a=0;a<seq1[i].size()+1;a++){
					delete [] tbtt[a];
			}			
			delete [] tbtt;


I found the answer here:
http://stackoverflow.com/questions/5822764/exception-stdbad-alloc-at-memory-location-in-a-loop

However if anyone knows any quicker ways to clean up allocated 2d arrays like this, I'd still be interested to hear them. Thanks.
Topic archived. No new replies allowed.