Can somebody tell me whats wrong with this 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>

using namespace std;

//overload stream insertion and extraction operators

class vector{
private:
	int *arr;
	int max_size;
	int elements;
public:
	//default constructor initializes everything to zero/null
	vector();
	//initializes the max_size to size and allocates this amount of memory
	vector(int size);
	//initializes the max_size to size and allocates this amount of memory and initializes every element to initial value
	vector(int size, int &initialValue);
	//copy constructor
	vector(const vector &v);
	//destructor
	~vector();
	//returns the max_size
	int capacity();
	//returns the no of elements
	int size();
	//tells whether it is empty or not means there are no elements i.e. elements=0
	bool empty();
	//resizes it to the size passed as parameter and preserves old elements
	void resize(int size);
	//deletes last element of vector
	bool deleteElement();		
	//add the element at the end
	bool addElement(int data);	
	
	int& operator[] (int index);
	vector& operator=(vector &obj);
	
};

vector::vector()
{
	arr=nullptr;
	max_size=0;
	elements=0;
}

vector::vector(int size)
{
	arr=new int[size];
}

vector::vector(int size, int &initialValue)
{
	arr=new int[size];
	arr[0]=initialValue;

}

vector::vector(const vector&v)
{
	vector aahm;
	aahm.max_size=max_size;
	aahm.elements=elements;
	aahm.arr=new int[aahm.max_size];
	for(int i=0;i<aahm.max_size;i++)
		aahm.arr[i]=arr[i];

}

vector::~vector()
{
	delete [] arr;
}

int vector::capacity()
{
	return max_size;
}

int vector::size()
{
	return elements;
}

bool vector::empty()
{
	if(elements ==0)
	return false ;

	return true;
}

void vector::resize(int size)
{
	vector v1;
	v1.arr = new int[size];
	for(int i=0;i<size;i++)
		v1.arr[i]=arr[i];
}

bool vector::deleteElement()
{
	if(!empty())
	{
	vector v1;
	v1.max_size=max_size;
	v1.elements=elements;
	v1.arr= new int [max_size-1];
	for(int i=0;i<max_size-1;i++)
		v1.arr[i]=arr[i];
	return true;
	}
	return false;
}

bool vector::addElement(int data)
{
	vector v1;
	v1.max_size=max_size;
	v1.elements=elements;
	v1.arr=new int [max_size+1];
	for(int i=0;i<max_size+1;i++)
		v1.arr[i]=arr[i];
	v1.arr[max_size+1]=data;
	return true;
}

int& vector::operator[] (int index)
{
	if(index < max_size && index >= 0)
	{
		return arr [index];
	}
}


vector& vector::operator=(vector &obj)
{
	if(this != &obj)
	{
		max_size=obj.max_size;
		elements=obj.elements;
	}
	return (*this);
}

ostream& operator<<(ostream& os, vector& obj)
{
	for(int i = 0; i<obj.capacity(); i++)
		os << obj[i] << endl;
	return os;
}

istream& operator>>(istream& os,vector& obj)
{
	for(int i = 0; i<obj.capacity(); i++)
		os >> obj[i];
	return os;
}

int main()
{
	vector v1;
	vector v2(10);
	vector v3(v2);
	vector v4;
	v4=v3;
	int x=v2.capacity();
	int y=v3.size();
	for(int i=0;i<x;i++)
	{
		cin>>v2[i];
	}
	for(int i=0;i<x;i++)
	{
		cout<<v2[i];
	}
	system("pause");
}



gives warning on line 135 and when i run it the program crashes too and gives a debug error. I'm using Visual Studio 2010
Last edited on
Hi,

Your code has many bugs, that include copy constructor, assignment operator, vector(int size, int &initialValue) methods.
A warning is not bad, but your bugs cause memory problems.
There are many sloppy mistakes, but one at a time.

Your class has three data members
1
2
3
	int *arr;
	int max_size;
	int elements;

There certain things have to be true about them. They have to be true at the end of each constructor, the beginning of the destructor and the entry/exit of all other functions. We call these invariants. In your case:
1. arr must point to a block of allocated memory (your code doesn't cater for a NULL value)
2. max_size is zero or a positive number
3. elements is in the range [0, max_size)

Lets consider vector::vector(int size). It clearly doesn't respect these invariants.
Line 48: max_size and elements are not updated.
Line 53: max_size and elements are not updated.
Line 60: all updates are to a local 'vector' that goes out of scope at the end of the function.
Line 86: returns true when not empty.
Line 94: all updates are to a local 'vector' that goes out of scope at the end of the function.
Line 102: all updates are to a local 'vector' that goes out of scope at the end of the function.
Line 117: all updates are to a local 'vector' that goes out of scope at the end of the function.
Line 129: You let the compiler return junk when index is out of range.
Line 138: arr is not updated.
Topic archived. No new replies allowed.