I am using Visual Studio 2010 C++ and its giving me an error "bad allocation" if I increase the array size t0 40 000 by 35 000, I am new to C++ not sure what to do.
// SWTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#pragma warning(disable: 4307)
using namespace std;
double getMatchscore(char,char);
double getMaximumvalue(double,double,double);
string reverseString(string);
int _tmain(int argc, _TCHAR* argv[])
{
ifstream myfile ("C:/Temp/sequencestest1.txt");
//Declare the two sequences S1 and S2 to be used.
string S1 = "";
string S2 = "";
string seperator = "";
if (myfile.is_open()){
if(! myfile.eof()){
getline(myfile,S1);
}
if(! myfile.eof()){
getline(myfile,seperator);
}
if(! myfile.eof()){
getline(myfile,S2);
}
myfile.close();
}
string aligned_S1 = "";
string aligned_S2 = "";
//Declare the sequences lengths.
const int n = S1.length();
const int m = S2.length();
int p, q;
int i_max, j_max;
int max_Value;
//Delcare the similarity matrix
double** D = 0;
char** tracebackMatrix;
D = new double*[m+1];
for( int i = 0; i <= m; i++ ) {
try
{
D[i] = new double[n+1];
}
catch(std::bad_alloc& er)
{
delete [] *D;
delete [] D ;
std::cout << er.what() << std::endl;
}
}
tracebackMatrix = new char*[m+1];
for( int i = 0; i <= m; i++ ) tracebackMatrix[i] = new char[n+1];
//Declare the scoring matrix to be used
double matchscore = 2;
double mismatchscore = -1;
double gapscore = -1;
int diagalignscore;
int verticalalignscore;
int horizontalalignscore;
//Initialise the scoring matrix
for(int i = 0; i <= m; i++)
{
D[i][0] = 0;
}
for(int j = 1; j <= n; j++)
{
D[0][j] = 0;
}
i_max = 0;
j_max = 0;
max_Value = 0;
//Fill the other entries of the scoring matrix
for(int i = 1; i <= m; i++)
{
for(int j = 1; j <= n; j++)
{
diagalignscore = getMatchscore(S1[j-1], S2[i-1]) + D[i-1][j-1];
verticalalignscore = D[i-1][j] + gapscore;
horizontalalignscore = D[i][j-1] + gapscore;
D[i][j] = getMaximumvalue(diagalignscore, verticalalignscore, horizontalalignscore);
//Keeping Track of the maximum entry in the matrix
if (D[i][j] > max_Value)
{
max_Value = D[i][j];
i_max = i;
j_max = j;
}
//Keeping track of the pointers in the matrix with 'S', to signal the stop in the traceback process.
if (D[i][j] == 0)
{
tracebackMatrix[i][j] = 'S';
}
else if (D[i][j] == diagalignscore)
{
tracebackMatrix[i][j] = '\\';
}
else if (D[i][j] == verticalalignscore)
{
tracebackMatrix[i][j] = '|';
}
else if (D[i][j] == horizontalalignscore)
{
tracebackMatrix[i][j] = '-';
}
}
}
p = i_max;
q = j_max;
//Doing the traceback here.
while ((tracebackMatrix[p][q] != 'S') && (p>0 && q > 0))
{
if (tracebackMatrix[p][q] == '|')
{
aligned_S1 = aligned_S1 + '-';
aligned_S2 = aligned_S2 + S2[p-1];
p = p-1;
}
else if (tracebackMatrix[p][q] == '\\')
{
aligned_S1 = aligned_S1 + S1[q-1];
aligned_S2 = aligned_S2 + S2[p-1];
p = p-1;
q = q-1;
}
else if (tracebackMatrix[p][q] == '-')
{
aligned_S2 = aligned_S2 + '-';
aligned_S1 = aligned_S1 + S1[q-1];
q = q-1;
}
}
cout << reverseString(aligned_S1) << endl;
cout << reverseString(aligned_S2) << endl;
for( int i = 0 ; i < n ; i++ ){
delete [] D[i] ;
}
delete [] D ;
return 0;
}
double getMatchscore(char a, char b)
{
double matchscore = 0;
if (a==b)
matchscore = 2;
else
matchscore = -1;
return matchscore;
}
double getMaximumvalue(double value1, double value2, double value3)
{
double max = value1;
if (max < value2)
{
max = value2;
}
if (max < value3)
{
max = value3;
}
if (max < 0)
{
max = 0;
}
return max;
}
string reverseString(string current)
{
string returnString = "";
int stringlength = current.length();
int temp = stringlength-1;
for (int i = 0; i < stringlength; i++)
{
returnString = returnString + current[temp];
temp = temp - 1;
}
return returnString;
}