HEAP CORRUPTION DETECTED

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
#include "stdio.h"
#include "conio.h"
#include "string"
#include "iostream"
using namespace std;

class MySimpleString
{
private:
	int m_iSoLuong;
	char* m_strChuoiTen;
public:
	MySimpleString()
	{
		m_iSoLuong = 0;
		m_strChuoiTen = NULL;
	}
	MySimpleString(const MySimpleString &mssStr)
	{
		m_iSoLuong = mssStr.m_iSoLuong;
		m_strChuoiTen = new char[m_iSoLuong + 1];
		strcpy(m_strChuoiTen, mssStr.m_strChuoiTen);
	}
	MySimpleString(char* ch)
	{
		m_iSoLuong = strlen(ch);
		m_strChuoiTen = new char [m_iSoLuong + 1];
		strcpy(m_strChuoiTen, ch);
	}
	MySimpleString(char ch, int iRepeat)
	{
		m_iSoLuong = iRepeat;
		m_strChuoiTen = new char[iRepeat+1];
		strnset(m_strChuoiTen, ch, iRepeat);
		m_strChuoiTen[iRepeat] = NULL;
	}
	~MySimpleString()
	{
		delete m_strChuoiTen;
	}

	void display()
	{
		printf("\n%d\n%s", m_iSoLuong, m_strChuoiTen);
	}

	bool operator == (MySimpleString mss)
	{
		return (strcmp(m_strChuoiTen, mss.m_strChuoiTen)==0)? true:false;
	}
	bool operator == (char* ch)
	{
		return (strcmp(m_strChuoiTen, ch)==0)? true:false;
	}
	bool operator > (MySimpleString mss)
	{
		return (strcmp(m_strChuoiTen, mss.m_strChuoiTen)>0)? true:false;
	}
	bool operator > (char* ch)
	{
		return (strcmp(m_strChuoiTen, ch)>0)? true:false;
	}
	bool operator < (MySimpleString mss)
	{
		return (strcmp(m_strChuoiTen, mss.m_strChuoiTen)<0)? true:false;
	}
	bool operator < (char* ch)
	{
		return (strcmp(m_strChuoiTen, ch)<0)? true:false;
	}
	char* operator + (MySimpleString mss)
	{
		return strcat(m_strChuoiTen, mss.m_strChuoiTen);
	}
	char* operator + (char* ch)
	{
		return strcat(m_strChuoiTen, ch);
	}
};

void main()
{
	MySimpleString mss("kaka");
	mss.display();

	mss+"kiki";
	mss.display();
	getch();
}


I get some trouble here:
"warning'strcpy' was declared deprecated"
when exit:
"HEAP CORRUPTION DETECTED: after Normal block (#48) at 0x000032E90.
CRT detected that the application wrote to memory after end of heap buffer."

I use VC++ 2005.
Help plz!!
You were just adding on the new string to the end of the existing string
and overflowing the buffer.
Also operator + should NOT affect/change either of it's operands.
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
class MySimpleString
{
private:
	int m_iSoLuong;
	char* m_strChuoiTen;
public:
	MySimpleString()
	{
		m_iSoLuong = 0;
        //GG - minor change - avoiding NULL pointers - makes it easier for operator+ 
        m_strChuoiTen = new char[1]; //GG - allocate one byte
        m_strChuoiTen[0]=0;//GG-Empty string

	}
	MySimpleString(const MySimpleString &mssStr)
	{
		m_iSoLuong = mssStr.m_iSoLuong;
		m_strChuoiTen = new char[m_iSoLuong + 1];
		strcpy(m_strChuoiTen, mssStr.m_strChuoiTen);
	}
	MySimpleString(char* ch)
	{
		m_iSoLuong = strlen(ch);
		m_strChuoiTen = new char [m_iSoLuong + 1];
		strcpy(m_strChuoiTen, ch);
	}
	MySimpleString(char ch, int iRepeat)
	{
		m_iSoLuong = iRepeat;
		m_strChuoiTen = new char[iRepeat+1];
		strnset(m_strChuoiTen, ch, iRepeat);
		m_strChuoiTen[iRepeat] = NULL;
	}
	~MySimpleString()
	{
		delete []m_strChuoiTen; //GG
	}

	void display()
	{
		printf("\n%d\n%s", m_iSoLuong, m_strChuoiTen);
	}

	
    //GG - Added assignment operator
    void operator = (const MySimpleString &mss)
	{
		m_iSoLuong = mss.m_iSoLuong;
        delete [] m_strChuoiTen;//GG - Delete current string - prevent memory leak
		m_strChuoiTen = new char[m_iSoLuong + 1];
		strcpy(m_strChuoiTen, mss.m_strChuoiTen);
		
	}

    
    bool operator == (MySimpleString mss)
	{
		return (strcmp(m_strChuoiTen, mss.m_strChuoiTen)==0)? true:false;
	}
	bool operator == (char* ch)
	{
		return (strcmp(m_strChuoiTen, ch)==0)? true:false;
	}
	bool operator > (MySimpleString mss)
	{
		return (strcmp(m_strChuoiTen, mss.m_strChuoiTen)>0)? true:false;
	}
	bool operator > (char* ch)
	{
		return (strcmp(m_strChuoiTen, ch)>0)? true:false;
	}
	bool operator < (MySimpleString mss)
	{
		return (strcmp(m_strChuoiTen, mss.m_strChuoiTen)<0)? true:false;
	}
	bool operator < (char* ch)
	{
		return (strcmp(m_strChuoiTen, ch)<0)? true:false;
	}
	
    
     //GG - Some changes made here
     //return type changed to allow chaining of + operator  
     MySimpleString operator + (const MySimpleString &mss)
	{      
        //in easy steps       
        MySimpleString mssTemp;
        //calculate ne string size
        int newSize = m_iSoLuong + mss.m_iSoLuong + 1;
        char *temp = new char[newSize];
        //copy and cat
        strcpy(temp,m_strChuoiTen);
        strcat(temp,mss.m_strChuoiTen);
        
        //copy over the particulars
        mssTemp.m_strChuoiTen =temp;
        mssTemp.m_iSoLuong = newSize-1;

        return mssTemp;		
        
	}
	
    
    //GG - Some changes made here
     //return type changed to allow chaining of + operator
    MySimpleString operator + (char* ch)
	{
		//in easy steps       
        MySimpleString mssTemp;
        //calculate ne string size
        int newSize = m_iSoLuong + strlen(ch) + 1;
        char *temp = new char[newSize];
        //copy and cat
        strcpy(temp,m_strChuoiTen);
        strcat(temp,ch);
        
        mssTemp.m_strChuoiTen =temp;
        mssTemp.m_iSoLuong = newSize-1;

        return mssTemp;
	}
};

int main ()
{
  	MySimpleString mss, mss1;
	mss.display();

	 mss1 = mss+"kiki" + "Dee"; //operator+ and assignment operator test
	mss.display(); //check - mss should not have been changed
    cout << endl;
    mss1.display(); //mss1 should show kikiDee

    MySimpleString mss2(mss1);//copy constructor test
    mss2.display();

	getch();
}
Topic archived. No new replies allowed.