Heap corruption detected

I have

"HEAP CORRUPTION DETECTED after normal block at

CRT DETECTED that the application wrote to memory after end of heap buffer"

error Message

After I debugged I found that this error message occured at ~String() method(delete problem)
plz help me to corect this error.

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
this is the full code ################################   strng3.h

#ifndef _STRNG2_H_
#define _STRNG2_H_

class String
{
private:
	char *str;
	int len;
public:
	String(const char *s);
	String();
	String(const String &st);
	~String();
	int length() const {return len;}

	String & operator=(const String &st);
	String & operator=(const char *s);

	String & operator+(const String &st);
	String & operator+(const char *s);

	friend ostream & operator<<(ostream &os,const String &st);
	friend istream & operator>>(istream &is,String &st);
	friend String & operator+(const char *s,String &st);
};

#endif


################################ strng3.cpp

#include <iostream>
#include <cstring>
using namespace std;
#include "strng3.h"

String::String()
{
	len=0;
	str=new char[1];
	str[0]='\0';

}

String::String(const char *s)
{
	len=strlen(s);
	str=new char[len+1];
	strcpy(str,s);
}

String::String(const String &st)
{
	len=st.len;
	str=new char[len+1];
	strcpy(str,st.str);
}

String::~String()
{
	delete [] str;               // <-------------------------------------- here
}

String & String::operator =(const char *s)
{
	delete [] str;
	len=strlen(s);
	str=new char[len+1];
	strcpy(str,s);
	return *this;
}

String & String::operator =(const String &st)
{
	if(this==&st)
		return *this;
	delete [] str;
	len=st.len;
	str=new char[len+1];
	strcpy(str,st.str);
	return *this;
}

String & String::operator +(const String &st)
{
	for(int i=0;i<=st.len;i++)
		*(str+len+i)=*(st.str+i);
	len=len+st.len;
	return *this;	
}

String & String::operator +(const char *s)
{
	int slen=strlen(s);
	char * temp=new char[slen+1];
	strcpy(temp,s);
	for(int i=0;i<slen;i++)
		*(str+len+i)=*(temp+i);
	delete [] temp;
	return *this;
}

String & operator+(const char *s,String &st)
{
	String temp(s);
	st=temp+st;                    //<---------------------- here
	return st;
}


ostream & operator<<(ostream &os,const String &st)
{
	os<<st.str;
	return os;
}

istream & operator>>(istream &is,String &st)
{
	char temp[80];
	is.get(temp,80);
	if(is)
		st=temp;
	while(is && is.get()!='\n')
		continue;
	return is;
	
	/*
	char temp[20];
	is>>temp;
	char * ps=new char[strlen(temp)+1];
	strcpy(ps,temp);
	strcpy(st.str,ps);
	st.len=strlen(st.str);
	delete [] ps;
	return is;
	*/
}

#############################################

#include <iostream>
using namespace std;
#include "strng3.h"

int main()
{
	String s1("and I'm learnging c++");
	String s2="your name:";
	String s3;
	cout<<s2;
	cin>>s3;
	s2="my name is "+s3;            //<--------------- here
	cout<<s2<<".\n";
	s2=s2+s1;
	cout<<s2<<"\n";

	return 0;
}
Last edited on
Your operator+ overloads are seriously flawed.
To elaborate a bit more:

* + operators should be const, and should not change 'this'. If you have code that does c = a + b;, you don't expect 'a' to change, however with your operators 'a' would change. + operators should also return an object by value, not a reference.

* Your + overload on line 97 doesn't increase the size of the buffer to accomidate the new, longer string. Therefore you get buffer overflows / heap corruption.

* Your + overload on line 105 changes the size of the buffer, but only to make it long enough to hold the new string -- it does not make it large enough to hold both the new string and the old string (ie: it'd be find for assignment, but for appending, it's no good). So more heap corruption there.
Thank for all your help.

I found the solution.

Now it works with no heap corruption!

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
String & String::operator +(const String &st)
{
	char *temp=new char[len+st.len+1];
	strcpy(temp,str);
	for(int i = 0; i <= st.len; i++)
	{
		*(temp + len + i) = *(st.str + i);
	}
	len = len + st.len;
	delete [] str;
	str=new char[len+1];
	strcpy(str,temp);
	delete [] temp;
	return *this; 

}

String operator+(const char *s,String &st)
{
	String temp(s);
	return temp+st;
}


Last edited on
Topic archived. No new replies allowed.