Program keeps crashing

Every time I compile and execute my code it crashes, and I am unsure why this is. I think it may have something to do with my insert function but I can't figure out what exactly. Any help is appreciated. 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  // Container with dynamic storage
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cstdlib>	
#include <ctime>
using namespace std; 

class container {
	friend ostream& operator<<(ostream& out, container &);
	// Postcondition: displays # of values stored in the container, storage capacity of the contianer, and stored values in the container 
	//                in the following format: Array size = 3, capacity = 4, contents = 11, 22, 33 (see below sample program output

public:
	container();
	// Postcondition: set dynamic storage capacity to 1 and count to -1 where (count + 1) represents the actual values stored 
	//                 in the container. Notice that data member count is used as the subscript to access elements (actual values) stored 
	//                 in the dynamic array; thus (count + 1) represents the total # of values that are currently stored in the array
	
	container(int n);
	// Postcondition: set dynamic storage (data array) capacity to n and count to -1
	
	container(container &);
	// Programmer-supplied copy constructor is necessary to avoid memory leak and other side effect
	// Postcondition: a new container class object is created which is the same as the one passed to the function
	
	~container();
	// Programmer-supplied destructor is necessary to avoid memory leak
	// Postcondition: all dynamic memory locations have been returned back to the heap whenever a container object goes out of scope
	
	container operator=(const container &rhs);
	// Programmer-supplied overloaded assignment is necessary to avoid memory leak and other side effect
	// Postconditoin: the container object rhs is assigned to the calling object
   
	void insert(int);
	// Postcondition: if the container is not full, the value passed to the function is stored in  
	//			the first available element of the dynamic array. Otherwise the function calls the private 
	//         	       "allocate" member function requesting a new set of dynamic memory with twice the previous storage capacity 
	//			the insert function then increments count by 1 and insert the value into the new and larger array.
	
	void remove();
	//  Precondition: the data array must not be empty; i.e., count must be greater than or equal to 0.
	// Postcondition: if the container is not empty, then remove the most recently stored value ifrom the container and 
	//			decrement count by 1; otherwise, display the message "The container is empty; no action is taken!"
	
	int operator[](int sub);
	//  Precondition: value passed to the function must be a positive integer including 0
	// Postcondition: the value of stored in data[sub] is returned; if sub is out of range, display a message and terminate the program .

	bool isFull();
	// Postcondition: return true if the container is full; return false otherwise 

	bool isEmpty();
	// Postcondition: return true if the container is empty; return false otherwise

	int Capacity();
	// Notice uppercase 'C' to avoid conflict with data member named "capacity"
	// Postcondition: returns the current storage capacity of the container
	
	int size();
	// Postcondition: returns the # of elements (# of objects) currently stored in the container

	void resize(int n);
	// Postcondition: container (i.e., the dynamic array) is resized to n; contents of existing container have been copied to the new array; 
	// 			      old array is deleted to avoid memory leak.

private:
	void allocate();
	// Postcondition: 1) the capacity of the container has been doubled, 2) existing values in the existing array have been copied to 
	//				   the new and larger dynamic array, 3) memory of the old array has been deleted (returned to "heap").
			  
	int *data;	
	int capacity;		// indicates the storage capcity of the container, i.e., the size of the dynamic array		
   	int count;		// used as a subscript to index into the array; size = count + 1
};

ostream& operator<<(ostream &out, container& c1)
{
	out << endl << "Array size = " << c1.count + 1;
	out << endl << "Capacity = " << c1.Capacity();
	out << endl << "Contents = ";
	if(c1.isEmpty() == true)
	{
		out << "empty!";
	}
	else
	{
		for(int i =0; i < c1.Capacity(); i++)
			out << c1.data[i] << ", ";
	}

	return out;
}

container::container()
{	
	capacity = 1;
	count = -1;
	data = new int[capacity];
	assert (data != 0);
}

container::container(int n)
{
	capacity = n;
	count = -1;
}

container::container(container &cobj) 
{
	data = cobj.data;   
	data = new int[capacity]; 
	assert(data != 0); 
	
	capacity = cobj.capacity;
	count = cobj.count;
}

container::~container()
{
	delete data;
}

container container::operator=(const container& rhs)
{
	count = rhs.count;
	for(int i = 0; i < count; i++)
	{
		data[i] = rhs.data[i];
	}
	capacity = rhs.capacity;
}

void container::insert(int value) /// maybe problem with data array declared being declared incorrectly 
{
	if (!isFull())
	{
		data[++count] = value;		
		cout << setw(4) << value 
		<< " has been inserted in data[" << count << "]." << endl;
	}
	else 
	{
		cout << 
	"Attempts to insert a value into a full container; program is terminated!";
		exit (1);
	}
}

void container::remove()
{
	if (count >= 0)
	{
		--count;
	}
	else
	{
		cout << endl << "Container is empty; program is terminated!";
		exit(1);
	}
}

int container::operator[](int sub)
{
	if(sub <= capacity)
	{
		return data[sub];
	}
	else
	{
		cout << endl << "Sub is out of range; program is terminated!";
		exit(1);
	}
}

bool container::isFull()
{
	if (count == capacity-1)
	{
		return true;
		allocate();
	}
	else
		return false;
}

bool container::isEmpty()
{
	if (count == -1)
		return true;
	else
		return false;
}

int container::Capacity()
{
	return capacity;
}

int container::size()
{
	return (count + 1);
}

void container::resize(int n)
{
	int *temptr;
	temptr = new int[n];
	for(int i = 0; i < n; i++)
	{
		temptr[i] = data[i];
	}
	delete data;
	data = temptr;
}

void container::allocate()
{
	capacity = capacity * 2;
	
	resize(capacity);
	
	delete data;
}

int main()
{
	container c1(16), c2(c1), c3;
	int n = 0;
	
	cout << endl << "Before inserting any value into the c1 container";
	cout << endl << c1;
	
	cout << endl << "Container capacity = " << c1.Capacity() <<
	" Size or actual # of values stored in container = " << c1.size();
	
	for (int i = 0; i < c1.Capacity(); i++)
		c1.insert(n+11);
	
	cout << endl << "Container c1: ";
	cout << endl << c1;
	
	cout << endl << "Declared c2 as a copy of c1 using the copy constructor.";
	cout << endl << "Container c2: ";
	cout << endl << c2;
	
	cout << endl << "Declared c3 and assign c2 to c3 with overloaded assignment operator.";
	cout << endl << "Container c3: ";
	c3 = c2;
	cout << endl << c3;
	
	
	c3.resize(4);
	cout << endl << "Resized c3 to size = 4";
	cout << endl << "Container c3: ";
	cout << endl << c3;
	
	c3.resize(7);
	cout << endl << "Resize c3 to size = 7";
	cout << endl << "Container c3: ";
	cout << endl << c3;
	
	cout << endl << "Finally, we display the address of three containers: ";
	cout << endl << "Address of c1 = &c1 = " << &c1;
	cout << endl << "Address of c2 = &c2 = " << &c2;
	cout << endl << "Address of c3 = &c3 = " << &c3;
	
	return 0;
}
Do yourself a favor and learn how to debug, otherwise it gets ugly.

Your problem is here.

container c1(16), c2(c1), c3;

1
2
3
4
5
6
7
8
9
container::container(container &cobj)
{
	data = cobj.data;
	data = new int[capacity]; // capacity is not initialized. So it crashes
	assert(data != 0);

	capacity = cobj.capacity;
	count = cobj.count;
}


The reason is simple. the object "c1" made the capacity equal to 16, but that is only for c1. So when you then create another object, "c2", The capacity is not 16, it's garbage (not initialized)

Edit: You can solve it by changing the line to this -

data = new int[cobj.capacity];
Last edited on
Topic archived. No new replies allowed.