Program crashing while using destructor

Hello, I wrote a code but it's crashing when i am using the DESTRUCTOR .
Please help me fixing this :3

My Program has :
1.header file
2.main program
3.header definitions.

Thanks

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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  Hello, I wrote a code but it's crashing when i am using the DESTRUCTOR .
Please help me fixing this :3

My Program has : 
1.header file
2.main program
3.header definitions.

Thanks

[code]
  #ifndef TEXT_H
#define TEXT_H
#include <iostream>
class Text
{
private:
	char *text;
public:
	Text();
	Text(const Text&);
	Text(const char*);
	friend std::ostream& operator<<(std::ostream&, const Text&);
	void set(const char*);
	void set(const Text&);
	Text & operator=(const Text & t);
	void append(const char*);
	void append(const Text &);
	int length() const;
	bool isEmpty() const;
	~Text();
};


#endif


#include "Text.h"
//#include "Menu.h"
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

int main()
{
	Text t1("Welcome to C++");
	Text t2;
	Text t3(t1);
	cout << "t1:" << t1 << endl;
	cout << "t2:" << t2 << endl;
	cout << "t3:" << t3 << endl;

	t2.set(" Programming");
	cout << "t2:" << t2 << endl;
	t3.set(t1);
	cout << "t3:" << t3 << endl;

	t1.append(" Programming");
	cout << "t1:" << t1 << endl;

	t3.append(t2);
	cout << "t3:" << t3 << endl;
	_getch();
	return 0;
}

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

Text::Text()
{
	this->text = nullptr;
	//text = new char();
}
Text::Text(const Text &t)
{
	int l = strlen(t.text);
	text = new char[l + 1]();
	for (int i = 0; i <= l + 1; i++)
	{
		text[i] = t.text[i];
	}

}
Text::Text(const char *t)
{
	int l = strlen(t);
	text = new char[l + 1];
	for (int i = 0; i <= l + 1; i++)
	{
		text[i] = t[i];
	}
}
std::ostream& operator<<(std::ostream& os, const Text& t) {
	if (t.text == nullptr) {
		os << "";
	}
	else {
		os << t.text;
	}

	return os;
}
void Text::set(const char *t)
{
	int l = strlen(t);
	text = new char[l + 1];
	for (int i = 0; i <= l + 1; i++) {

		text[i] = t[i];
	}
}
void Text::set(const Text &t) {
	int l = strlen(t.text);
	text = new char[l + 1];
	for (int i = 0; i <= l + 1; i++) {
		text[i] = t.text[i];
	}

}
Text& Text::operator=(const Text &t) {
	int l = strlen(t.text);
	text = new char[l + 1]();
	for (int i = 0; i <= l + 1; i++)
	{
		text[i] = t.text[i];
	}
	return *this;
}
void Text::append(const char *t)
{
	int l = strlen(text), r = strlen(t);
	for (int i = 0; i <= r + 1; i++)
	{
		text[l + i] = t[i];
	}
}
void Text::append(const Text &t)
{
	int l = strlen(text), r = strlen(t.text);

	for (int i = 0; i <= r + 1; i++)
	{
		text[l + i] = t.text[i];
	}
}
int Text::length() const
{
	int l = strlen(text);
	return l;
}
bool Text::isEmpty() const
{
	if (text != nullptr)
	{
		return false;
	}
	else
	{
		return true;
	}
}
Text::~Text()
{
	cout << "PEHLE" << endl;
	if (text!=nullptr)
	{	
		delete[] text;
	}
	cout << "Baad Mein" << endl;
}

 
in both set(), append(), constructors and assignment operator
1
2
3
4
5
6
	int l = strlen(t);
	text = new char[l + 1];
	for (int i = 0; i <= l + 1; i++) {

		text[i] = t[i];
	}
count carefully, you're going out of bounds
1
2
3
4
for (int i = 0; i <= l + 1; i++)
{
    text[i] = t.text[i];
}


You write to memory that you don't own. If the text contains "Welcome to C++" length = 14 and you allocate correctly 15 chars for the new text. However i <= 15 means you write to text[15] but text[14] is the last you own. Another problem is that you don't delete the old memory before you allocate new memory thus leaking memory.
I tried to fix it guys !
but now the program just r
Thanks for help , the error was in the logic too :)
and copying array to "L" not l+1 :p
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
#include "Text.h"
#include <string>
#include <iostream>
#include <conio.h>
using namespace std;

Text::Text()
{
	this->text = nullptr;
	//text = new char();
}
Text::Text(const Text &t)
{
	int l = strlen(t.text);
	text = new char[l + 1]();
	for (int i = 0; i <= l ; i++)
	{
		text[i] = t.text[i];
	}

}
Text::Text(const char *t)
{
	int l = strlen(t);
	text = new char[l + 1];
	for (int i = 0; i <= l ; i++)
	{
		text[i] = t[i];
	}
}
std::ostream& operator<<(std::ostream& os, const Text& t) {
	if (t.text == nullptr) {
		os << "";
	}
	else {
		os << t.text;
	}

	return os;
}
void Text::set(const char *t)
{
	int l = strlen(t);
	text = new char[l + 1];
	for (int i = 0; i <= l ; i++) {

		text[i] = t[i];
	}
}
void Text::set(const Text &t) {
	int l = strlen(t.text);
	text = new char[l + 1];
	for (int i = 0; i <= l ; i++) {
		text[i] = t.text[i];
	}

}
Text& Text::operator=(const Text &t) {
	int l = strlen(t.text);
	text = new char[l + 1]();
	for (int i = 0; i <= l ; i++)
	{
		text[i] = t.text[i];
	}
	return *this;
}
void Text::append(const char *t)
{
	char *textcpy;
	int l = strlen(text), r = strlen(t);
	textcpy = new char[l + r + 1];

	for (int i = 0; i <= l; i++)
	{
		textcpy[i] = text[i];
	}

	for (int i = 1; i <= r; i++)
	{
		textcpy[l + i] = t[i];
	}
	text = new char[l + r + 1];
	for (int i = 0; i <= l + r; i++) { text[i] = textcpy[i]; }

}


void Text::append(const Text &t)
{
	char *textcpy;
	int l = strlen(text);
	int r = strlen(t.text);
	textcpy = new char[l + r + 1];

	for (int i = 0; i <= l; i++)
	{
		textcpy[i] = text[i];
	}

	for (int i = 1; i <= r; i++)
	{
		textcpy[l + i] = t.text[i];
	}
	text = new char[l + r + 1];
	for (int i = 0; i <= l + r; i++) { text[i] = textcpy[i]; }
}
int Text::length() const
{
	int l = strlen(text);
	return l;
}
bool Text::isEmpty() const
{
	if (text != nullptr)
	{
		return false;
	}
	else
	{
		return true;
	}
}
Text::~Text()
{
	cout << "PEHLE" << endl;
	if (text!=nullptr)
	{	
		delete[] text;
	}

	cout << "Baad Mein" << endl;
}


Now the program don't stand :3 ! i can't see the CMD window once i run the program , it vanish within a sec. :3
Last edited on
Can you show us the new code please.
i can't see the CMD window once i run the program , it vanish within a sec. :3


http://www.cplusplus.com/forum/beginner/1988/
you shill have the memory leaks, and need to guard against self-assignment
Topic archived. No new replies allowed.