deleting a dynamically located array cause crashes

this is the code that i wrote, am suppose to delete the arrays at the end to free the allocated memory by using a distructor, but when i do so the whole program crashes and without it, it runs flawlessly, it gives the error wntdll.pdb not loaded, but on other compilers it says double memory feed.
btw it works fine on some compilers, but not on Visual studio, and i am supposed to use VS.

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
#include <iostream>
using namespace std;
class myfunnyarray {
	int Size = 0;
	int *array;
public:
	myfunnyarray(int size);
	myfunnyarray(int size, int value);
	friend ostream& operator << (ostream& lhs, myfunnyarray& rhs);
	myfunnyarray operator + (myfunnyarray& rhs);
	myfunnyarray operator - (int no);
	myfunnyarray() {};
	int& operator [] (int index);
	friend myfunnyarray& operator ++(myfunnyarray& arg);
	myfunnyarray operator ++(int);
	~myfunnyarray();

};
myfunnyarray myfunnyarray::operator ++( int) {//post increment
	myfunnyarray temp(Size,1);//temp obj to work onn
	temp.Size = Size;
	int* new_array = new int[Size];

	for (int i = 0; i < Size; i++) {
		new_array[i] = array[i];
		temp.array[i] = new_array[i];
	}
	for (int i = 0; i < Size; i++) {
		array[i]++;
	}
	//delete[]new_array;
	return temp;
}
myfunnyarray& operator ++(myfunnyarray& arg) {
	for (int i = 0; i < arg.Size; i++) {
		arg.array[i]++;
	}
	return arg;
}
myfunnyarray::myfunnyarray(int size) {
	array = new int[size];
	for (int i = 0; i < size; i++) {
		array[i] = 0;
	}
	Size = size;
}
myfunnyarray::myfunnyarray(int size, int value) {
	array = new int[size];
	for (int i = 0; i < size; i++) {
		array[i] = value;
	}
	Size = size;
}
ostream& operator << (ostream& lhs, myfunnyarray& rhs) {
	for (int i = 0; i < rhs.Size; i++) {
		lhs << rhs.array[i] << " ";
	}
	return lhs;
}
myfunnyarray myfunnyarray ::operator + (myfunnyarray& rhs) {
	myfunnyarray obj;
	int array_size = Size + rhs.Size;
	obj.Size = array_size;
	obj.array = new int[obj.Size];
	for (int i = 0; i < Size; i++) {
		obj.array[i] = array[0];
	}for (int i = Size; i < array_size; i++) {
		obj.array[i] = rhs.array[0];
	}
	return obj;
}
int& myfunnyarray:: operator [] (int index) {
	if (index > -1 && index < Size) {
		return array[index];
	}
	else { cerr << "invalid index"; exit(-1); }
}
myfunnyarray myfunnyarray ::operator - (int no) {
	myfunnyarray obj;
	obj.Size = Size;
	obj.array = new int[obj.Size];

	for (int i = 0; i < no; i++) {
		obj.array[i] = array[i] - 3;
	}for (int i = 3; i < Size; i++) {
		obj.array[i] = array[i]-no;
	}
	return obj;
}
myfunnyarray::~myfunnyarray() {
	if(array!=nullptr)delete[] array;
}
bool checkifzero(myfunnyarray& arg, int index) {
	if (arg[index] == 0)
		return true;
	else
		return false;
}
int main() {
	myfunnyarray a(3);		
	cout << a << endl;		
	myfunnyarray b(4, 5);		
	cout << b << endl;		

	myfunnyarray c = a + b; 
	cout << c << endl;				

	c[1] = 33;			
	if (checkifzero(c, 1) == false)
		cout << c << endl;			

	myfunnyarray d = c++;
	cout << d << endl;			
	cout << c << endl;			

	myfunnyarray e = ++(++c);
	cout << e << endl;			
	cout << c << endl;			

	myfunnyarray f = c - 3;	// f[i] = c[i]-3;
	cout << f << endl;			
	cout << c << endl;			

	(c = b) = a;
	cout << c << endl;			

	cout << a << "-" << b << "-" << c;	
	return 0;
}

Last edited on
That code is unreadable! Use code tags and format your code.

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
#include <iostream>
using namespace std;

class myfunnyarray {
	int Size = 0;
	int* array;

public:
	myfunnyarray(int size);
	myfunnyarray(int size, int value);
	friend ostream& operator << (ostream& lhs, myfunnyarray& rhs);
	myfunnyarray operator + (myfunnyarray& rhs);
	myfunnyarray operator - (int no);
	myfunnyarray() {};
	int& operator [] (int index);
	friend myfunnyarray& operator ++(myfunnyarray& arg);
	myfunnyarray operator ++(int);
	~myfunnyarray();
};

myfunnyarray myfunnyarray::operator ++(int) {//post increment
	myfunnyarray temp(Size, 1);//temp obj to work onn
	temp.Size = Size;
	int* new_array = new int[Size];

	for (int i = 0; i < Size; i++) {
		new_array[i] = array[i];
		temp.array[i] = new_array[i];
	}

	for (int i = 0; i < Size; i++) {
		array[i]++;
	}

	//delete[]new_array;
	return temp;
}

myfunnyarray& operator ++(myfunnyarray& arg) {
	for (int i = 0; i < arg.Size; i++) {
		arg.array[i]++;
	}

	return arg;
}

myfunnyarray::myfunnyarray(int size) {
	array = new int[size];
	for (int i = 0; i < size; i++) {
		array[i] = 0;
	}

	Size = size;
}

myfunnyarray::myfunnyarray(int size, int value) {
	array = new int[size];
	for (int i = 0; i < size; i++) {
		array[i] = value;
	}

	Size = size;
}

ostream& operator << (ostream& lhs, myfunnyarray& rhs) {
	for (int i = 0; i < rhs.Size; i++) {
		lhs << rhs.array[i] << " ";
	}

	return lhs;
}

myfunnyarray myfunnyarray ::operator + (myfunnyarray& rhs) {
	myfunnyarray obj;
	int array_size = Size + rhs.Size;

	obj.Size = array_size;
	obj.array = new int[obj.Size];

	for (int i = 0; i < Size; i++) {
		obj.array[i] = array[0];
	}

	for (int i = Size; i < array_size; i++) {
		obj.array[i] = rhs.array[0];
	}

	return obj;
}

int& myfunnyarray:: operator [] (int index) {
	if (index > -1 && index < Size) {
		return array[index];
	} else {
		cerr << "invalid index";
		exit(-1);
	}
}

myfunnyarray myfunnyarray ::operator - (int no) {
	myfunnyarray obj;

	obj.Size = Size;
	obj.array = new int[obj.Size];

	for (int i = 0; i < no; i++) {
		obj.array[i] = array[i] - 3;
	}

	for (int i = 3; i < Size; i++) {
		obj.array[i] = array[i] - no;
	}

	return obj;
}

myfunnyarray::~myfunnyarray() {
	if (array != nullptr)
		delete[] array;
}

bool checkifzero(myfunnyarray& arg, int index) {
	if (arg[index] == 0)
		return true;
	else
		return false;
}

int main() {
	myfunnyarray a(3); // should create an array containing 3 integers each initialized to zero

	cout << a << endl; // should print: 0,0,0

	myfunnyarray b(4, 5); // should create an array containing 4 integers each initialized to five

	cout << b << endl; // should print: 5,5,5,5

	myfunnyarray c = a + b; // c should contain 7 elements: first 3 intialized to 0, last four to 5.

	cout << c << endl; // should print: 0,0,0,5,5,5,5

	c[1] = 33; // should assign value 33 to the element at index 1
	if (checkifzero(c, 1) == false)
		cout << c << endl; // should print: 0,33,0,5,5,5,5

	myfunnyarray d = c++;

	cout << d << endl; // should print: 0,33,0,5,5,5,5
	cout << c << endl; // should print: 1,34,1,6,6,6,6

	myfunnyarray e = ++(++c);

	cout << e << endl; // should print: 3,36,3,8,8,8,8
	cout << c << endl; // should print: 3,36,3,8,8,8,8

	myfunnyarray f = c - 3; // f[i] = c[i]-3;

	cout << f << endl; // should print: 0,33,0,5,5,5,5
	cout << c << endl; // should print: 3,36,3,8,8,8,8//there was a mistake here,sir wrote 38 instead of36

	(c = b) = a;
	cout << c << endl; // should print: 0,0,0

	cout << a << "-" << b << "-" << c; // should print 0,0,0-5,5,5,5-0,0,0
	return 0;
}


The main issue is that you need a copy constructor and a copy assignment=

There are other issues. As a first re-factor consider:

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
#include <iostream>
#include <utility>
#include <algorithm>
using namespace std;

class myfunnyarray {
	size_t Size {};
	int* array {};

public:
	myfunnyarray() {}
	myfunnyarray(size_t size);
	myfunnyarray(size_t size, int value);
	myfunnyarray(const myfunnyarray& rhs);
	~myfunnyarray();

	myfunnyarray& swap(myfunnyarray& rhs);
	myfunnyarray& operator=(const myfunnyarray rhs);
	myfunnyarray operator+(const myfunnyarray& rhs) const;
	myfunnyarray operator-(int no) const;

	int& operator[](size_t index);
	const int& operator[](size_t index) const;

	myfunnyarray operator++(int);
	myfunnyarray& operator++();

	friend ostream& operator<<(ostream& lhs, const myfunnyarray& rhs);
};

myfunnyarray& myfunnyarray::swap(myfunnyarray& rhs) {
	std::swap(rhs.Size, Size);
	std::swap(rhs.array, array);

	return *this;
}

myfunnyarray& myfunnyarray::operator=(myfunnyarray rhs) {
	swap(rhs);

	return *this;
}

myfunnyarray::myfunnyarray(const myfunnyarray& rhs) : Size(rhs.Size), array(new int[rhs.Size]) {
	std::copy_n(rhs.array, Size, array);
}

myfunnyarray myfunnyarray::operator ++(int) {//post increment
	myfunnyarray temp(*this);

	for (size_t i = 0; i < Size; ++i)
		++array[i];

	return temp;
}

myfunnyarray& myfunnyarray::operator++() {
	for (size_t i = 0; i < Size; ++i)
		++array[i];

	return *this;
}

myfunnyarray::myfunnyarray(size_t size) : Size(size), array(new int[size] {}) {}

myfunnyarray::myfunnyarray(size_t size, int value) : Size(size), array(new int[size]) {
	std::fill_n(array, Size, value);
}

ostream& operator << (ostream& lhs, const myfunnyarray& rhs) {
	for (size_t i = 0; i < rhs.Size; ++i)
		lhs << rhs.array[i] << ' ';

	return lhs;
}

myfunnyarray myfunnyarray ::operator+(const myfunnyarray& rhs) const {
	myfunnyarray obj(rhs.Size + Size);

	for (size_t ind = 0; ind < Size; ++ind)
		obj.array[ind] = array[ind];

	for (size_t ind = 0; ind < rhs.Size; ++ind)
		obj.array[ind + Size] = rhs.array[ind];

	return obj;
}

int& myfunnyarray:: operator[] (size_t index) {
	if (index < Size)
		return array[index];

	cerr << "invalid index";
		exit(-1);
}

const int& myfunnyarray:: operator[] (size_t index) const {
	if (index < Size)
		return array[index];

	cerr << "invalid index";
	exit(-1);
}

myfunnyarray myfunnyarray ::operator-(int no) const {
	myfunnyarray obj (Size);

	for (size_t i = 0; i < Size; ++i)
		obj.array[i] = array[i] - no;

	return obj;
}

myfunnyarray::~myfunnyarray() {
	delete[] array;
}

bool checkifzero(myfunnyarray& arg, int index) {
	return arg[index == 0];
}

int main() {
	myfunnyarray a(3); // should create an array containing 3 integers each initialized to zero

	cout << a << '\n'; // should print: 0,0,0

	myfunnyarray b(4, 5); // should create an array containing 4 integers each initialized to five

	cout << b << '\n'; // should print: 5,5,5,5

	myfunnyarray c = a + b; // c should contain 7 elements: first 3 intialized to 0, last four to 5.

	cout << c << '\n'; // should print: 0,0,0,5,5,5,5

	c[1] = 33; // should assign value 33 to the element at index 1

	if (checkifzero(c, 1) == false)
		cout << c << '\n'; // should print: 0,33,0,5,5,5,5

	myfunnyarray d = c++;

	cout << d << '\n'; // should print: 0,33,0,5,5,5,5
	cout << c << '\n'; // should print: 1,34,1,6,6,6,6

	myfunnyarray e = ++(++c);

	cout << e << '\n'; // should print: 3,36,3,8,8,8,8
	cout << c << '\n'; // should print: 3,36,3,8,8,8,8

	myfunnyarray f = c - 3; // f[i] = c[i]-3;

	cout << f << '\n'; // should print: 0,33,0,5,5,5,5
	cout << c << '\n'; // should print: 3,36,3,8,8,8,8//there was a mistake here,sir wrote 38 instead of36

	(c = b) = a;
	cout << c << '\n'; // should print: 0,0,0

	cout << a << "-" << b << "-" << c << '\n'; // should print 0,0,0-5,5,5,5-0,0,0
}



0 0 0
5 5 5 5
0 0 0 5 5 5 5
0 33 0 5 5 5 5
0 33 0 5 5 5 5
1 34 1 6 6 6 6
3 36 3 8 8 8 8
3 36 3 8 8 8 8
0 33 0 5 5 5 5
3 36 3 8 8 8 8
0 0 0
0 0 0 -5 5 5 5 -0 0 0


which seems to be what is expected.
thanks alot, but the thing is we r not supposed to use any other library, just the basic iostream library is allowed
OK. I only used the stl functions in 3 places - which are easily removed:

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
#include <iostream>
//#include <utility>
//#include <algorithm>
using namespace std;

class myfunnyarray {
	size_t Size {};
	int* array {};

public:
	myfunnyarray() {}
	myfunnyarray(size_t size);
	myfunnyarray(size_t size, int value);
	myfunnyarray(const myfunnyarray& rhs);
	~myfunnyarray();

	myfunnyarray& swap(myfunnyarray& rhs);
	myfunnyarray& operator=(const myfunnyarray rhs);
	myfunnyarray operator+(const myfunnyarray& rhs) const;
	myfunnyarray operator-(int no) const;

	int& operator[](size_t index);
	const int& operator[](size_t index) const;

	myfunnyarray operator++(int);
	myfunnyarray& operator++();

	friend ostream& operator<<(ostream& lhs, const myfunnyarray& rhs);
};

myfunnyarray& myfunnyarray::swap(myfunnyarray& rhs) {
	//std::swap(rhs.Size, Size);
	//std::swap(rhs.array, array);

	const auto s {rhs.Size};
	rhs.Size = Size;
	Size = s;

	const auto a {rhs.array};
	rhs.array = array;
	array = a;

	return *this;
}

myfunnyarray& myfunnyarray::operator=(myfunnyarray rhs) {
	swap(rhs);

	return *this;
}

myfunnyarray::myfunnyarray(const myfunnyarray& rhs) : Size(rhs.Size), array(new int[rhs.Size]) {
	//std::copy_n(rhs.array, Size, array);

	for (size_t i = 0; i < Size; ++i)
		array[i] = rhs.array[i];
}

myfunnyarray myfunnyarray::operator ++(int) {//post increment
	myfunnyarray temp(*this);

	for (size_t i = 0; i < Size; ++i)
		++array[i];

	return temp;
}

myfunnyarray& myfunnyarray::operator++() {
	for (size_t i = 0; i < Size; ++i)
		++array[i];

	return *this;
}

myfunnyarray::myfunnyarray(size_t size) : Size(size), array(new int[size] {}) {}

myfunnyarray::myfunnyarray(size_t size, int value) : Size(size), array(new int[size]) {
	//std::fill_n(array, Size, value);

	for (size_t i = 0; i < Size; ++i)
		array[i] = value;
}

ostream& operator << (ostream& lhs, const myfunnyarray& rhs) {
	for (size_t i = 0; i < rhs.Size; ++i)
		lhs << rhs.array[i] << ' ';

	return lhs;
}

myfunnyarray myfunnyarray ::operator+(const myfunnyarray& rhs) const {
	myfunnyarray obj(rhs.Size + Size);

	for (size_t ind = 0; ind < Size; ++ind)
		obj.array[ind] = array[ind];

	for (size_t ind = 0; ind < rhs.Size; ++ind)
		obj.array[ind + Size] = rhs.array[ind];

	return obj;
}

int& myfunnyarray:: operator[] (size_t index) {
	if (index < Size)
		return array[index];

	cerr << "invalid index";
		exit(-1);
}

const int& myfunnyarray:: operator[] (size_t index) const {
	if (index < Size)
		return array[index];

	cerr << "invalid index";
	exit(-1);
}

myfunnyarray myfunnyarray ::operator-(int no) const {
	myfunnyarray obj (Size);

	for (size_t i = 0; i < Size; ++i)
		obj.array[i] = array[i] - no;

	return obj;
}

myfunnyarray::~myfunnyarray() {
	delete[] array;
}

bool checkifzero(myfunnyarray& arg, int index) {
	return arg[index == 0];
}

int main() {
	myfunnyarray a(3); // should create an array containing 3 integers each initialized to zero

	cout << a << '\n'; // should print: 0,0,0

	myfunnyarray b(4, 5); // should create an array containing 4 integers each initialized to five

	cout << b << '\n'; // should print: 5,5,5,5

	myfunnyarray c = a + b; // c should contain 7 elements: first 3 intialized to 0, last four to 5.

	cout << c << '\n'; // should print: 0,0,0,5,5,5,5

	c[1] = 33; // should assign value 33 to the element at index 1

	if (checkifzero(c, 1) == false)
		cout << c << '\n'; // should print: 0,33,0,5,5,5,5

	myfunnyarray d = c++;

	cout << d << '\n'; // should print: 0,33,0,5,5,5,5
	cout << c << '\n'; // should print: 1,34,1,6,6,6,6

	myfunnyarray e = ++(++c);

	cout << e << '\n'; // should print: 3,36,3,8,8,8,8
	cout << c << '\n'; // should print: 3,36,3,8,8,8,8

	myfunnyarray f = c - 3; // f[i] = c[i]-3;

	cout << f << '\n'; // should print: 0,33,0,5,5,5,5
	cout << c << '\n'; // should print: 3,36,3,8,8,8,8//there was a mistake here,sir wrote 38 instead of36

	(c = b) = a;
	cout << c << '\n'; // should print: 0,0,0

	cout << a << "-" << b << "-" << c << '\n'; // should print 0,0,0-5,5,5,5-0,0,0
}

Topic archived. No new replies allowed.