Windows is triggering a breakpoint in my program

Hi all,

I have written a simple C++ program to implement the string class. This class has member functions for +operator and =operator overload in addition to the constructors that I have defined for it.

The program runs but it stops with an error message saying "windows has triggered a break point in my program.This may be due to the corruption of the heap which indicated a bug in the program or the dlls that it loads"

The program produces correct output but it stops before returning the handle to the OS. I would appreciate it if somebody could help me ..

Thanks,
Puntis
Last edited on
You'll have to post the code.
Following you could see my code:

//==============================================================

#pragma once
#include<iostream>
#include<cstring>
using std::cout;
using std::endl;

class MyCString{

private:

char* inputSTR;
size_t length;
size_t len1, len2, totalLength;


public:

MyCString(const char* STR=nullptr);
MyCString& operator=(const MyCString& mycstr);
void showSTR();
MyCString(const MyCString& mycstr1);
MyCString& operator+(const MyCString& mycstr);
~ MyCString();
};


//defining the constructor we define the sttring on the heap.
MyCString::MyCString(const char* STR):length(0), inputSTR(nullptr){

if (STR!=nullptr){
length = strlen(STR);
if (length>0){

inputSTR = new char[length+1];
strcpy_s(inputSTR, length+1, STR);
}
}
}//end of the constructor definition.



/*CSimpleString::CSimpleString(const CSimpleString& s)
{
len = s.len;
buff = new char[len+1];
strcpy_s(buff, len+1, s.buff);
}*/

//defining the copy constructor:
MyCString::MyCString(const MyCString& mycstr1){


inputSTR = new char[(mycstr1.length)+1];

strcpy_s(inputSTR,(mycstr1.length)+1, mycstr1.inputSTR);

}
//defining the assignment operator

MyCString& MyCString::operator= (const MyCString& mycstr){

//length = mycstr.length;

delete[] inputSTR;
inputSTR = new char (mycstr.length+1);

strcpy_s(inputSTR, (mycstr.length)+1, mycstr.inputSTR);

return *this;
}

MyCString& MyCString::operator+(const MyCString& mycstr){

//delete[] inputSTR;

//cout<<"this is:"<<this->inputSTR;
//cout<<"mycstr is:"<<mycstr.inputSTR;

len1 = this->length;
len2 = mycstr.length;
totalLength = len1+len2+2;
MyCString* concatedSTR;

concatedSTR->length = totalLength;
concatedSTR->inputSTR = new char[totalLength];
concatedSTR->inputSTR = strcat(this->inputSTR, mycstr.inputSTR);

// cout<<"test"<<concatedSTR.inputSTR ;
return *concatedSTR;
}

void MyCString::showSTR(){

cout<< "The string is: "<< inputSTR<<endl;

}

MyCString:: ~MyCString(){

delete[] inputSTR;

cout<<"Destructor is called."<<endl;

}

//==================================================================

int main(){

MyCString st1= "test1";
MyCString st2= "test2";
MyCString st3="";
MyCString st5="";


st1.showSTR();

st3 = st1;

st3.showSTR();

MyCString st4(st2);
st4.showSTR();

st5 = st1 + st2;

st5.showSTR();

system("PAUSE");

return 0;
}
Last edited on
One problem is operator +. concatedSTR never points to anything. Though in fact, it shouldn't be a pointer at all. If you allocated memory, you'd never delete it. Its fine to return by value.
I changed the return type of the operator+ function.So now it returns an object of the MyCString class type and no pointer. I figured out that the function perfectly runs as if I print out the concatedSTR in the function in has concatenated the two stings. But it never returns to the main program after and so it doesn't run the next line in main program which is st5.showSTR();
Last edited on
A problem in operator =. You wrote () instead of []. Now only one char is allocated.
Also, you're misusing strcat. It appends the 2nd string to the 1st one and the result is stored in 1st. You should first copy the 1st string with strcpy and then use strcat.
Thanks a lot Hamsterman. The problem is solved :)
Topic archived. No new replies allowed.