Iam getting the error Heap Corruption Detected if i run the following program. If i comment out the line delete[], iam not getting the error. Also if i change the outTxtStr = new char[outTxtLength + 1] to outTxtStr = new char[50] iam not getting the error. Does anybody have some idea in how to solve the problem? Please help me.
int outTxtLength;
char *outTxtStr;
int i,j = 0;
outTxtLength = inTxt->length();
outTxtStr = new char[outTxtLength + 1];
//outTxtStr = new char[50];
outTxtLength = inTxt->copy(outTxtStr,outTxtLength);
outTxtStr[outTxtLength] = '\0';
// Remove spaces between words i.e consecutive spaces
for( i=0; i < outTxtLength; i++)
{
if ( ( outTxtStr[i]==' ' )&&( outTxtStr[i+1]==' ' ) )
{
outTxtLength--;
memcpy( &outTxtStr[i],&outTxtStr[i+1], outTxtLength);
}
}
delete[] outTxtStr;