I'm trying to create my own string class, which I named CStr.
First, I defined it as such (I left out a lot of irrelevant code at the moment):
Code:
class CStr
{
char* pData;
int nLength;
int stlen(char* c){for(int i = 0; *c != '\0'; c++, i++); return i;}
char* stcpy(char*, const char*);
char* stcat(char*, const char*);
public:
CStr(char*);
void set(char* c){stcpy(pData, c);}
};
Then the member function definitions:
Code:
char* CStr::stcpy(char* dest, const char* src)
{
char* p;
p = dest;
if(nLength > 0)
{
delete [] pData;
pData = new char[stlen(src)];
p = dest = pData;
}
while(*(p++)=*(src++));
return dest;
}
CStr::CStr(char* c)
{
pData = new char[stlen(c)];
stcpy(pData, c);
nLength = stlen(c);
}
Now here's the code from the main() function.
Code:
char* p = "First input";
char q[256];
CStr c(p);
while(gets(q))
c.set(q);
My problem is that I get a "Debug Error" message (in VC++ 2003)) which states as follows:
DAMAGE: After Normal Block(#42) at XXXXX.
This occurs at the line "delete [] pData" in the definition of stcpy() when it is called by "c.set(q)".
Please tell me, what am I doing wrong here?