Segmentation fault is happening again and again.

I built a program of class datat structure and one of the assignment keep making segmentation fault again and again. Professor gave me a tip that I need to isolate the code to figure out what the problems are but refused giving more hints. I was able to figure out what the problems are but I couldn't get more information why those are taking place. resize and copying vector is making 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
161
162
163
164
165
166
167
168
169
170
171
  #ifndef VECTOR_H
#define VECTOR_H

using namespace std;

template <class T>
class Vector
{
public:
    
    typedef T * iterator;
    
    Vector();
    Vector(unsigned int size);
    Vector(unsigned int size, const T & initial);
    Vector(const Vector<T> & v);           // copy constructor
    ~Vector();
    
    unsigned int capacity() const;         // return capacity of vector (in elements)
    unsigned int size() const;             // return the number of elements in the vector
    bool empty() const;
    
    iterator begin();                      // return an iterator pointing to the first element
    iterator end();                        // return an iterator pointing to one past the last element
    T & front();                           // return a reference to the first element
    T & back();                            // return a reference to the last element
    void push_back(const T & value);       // add a new element
    void pop_back();                       // remove the last element
    
    void reserve(unsigned int capacity);   // adjust capacity
    void resize(unsigned int size);        // adjust size
    
    T & operator[](unsigned int index);    // return reference to numbered element
    Vector<T> & operator=(const Vector<T> &);
    
private:
    unsigned int my_size;
    unsigned int my_capacity;
    T * buffer;
};


template<class T>
Vector<T>::Vector()
{
    my_capacity = 0;
    my_size = 0;
    buffer = 0;
}

template<class T>
Vector<T>::Vector(const Vector<T> & v)
{
    my_size = v.my_size;
    my_capacity = v.my_capacity;
    buffer = new T[my_capacity];
    for (unsigned int i = 0; i , my_size; i++)
	buffer[i] = v.buffer[i]; 
}

template<class T>
Vector<T>::Vector(unsigned int size)
{
    my_capacity = size;
    my_size = size;
    buffer = new T[size];
}

template<class T>
Vector<T>::Vector(unsigned int size, const T & initial)
{
    my_size = size;
    my_capacity = size;
    buffer = new T [size];
    for (int i = 0; i < size; i++)
        buffer[i] = initial;
}

template<class T>
Vector<T> & Vector<T>::operator = (const Vector<T> & v)
{
    delete[ ] buffer;
    my_size = v.my_size;
    my_capacity = v.my_capacity;
    buffer = new T [my_size];
    for (int i = 0; i < my_size; i++)
        buffer[i] = v.buffer[i];
    return *this;
}

template<class T>
typename Vector<T>::iterator Vector<T>::begin()
{
    return buffer;
}

template<class T>
typename Vector<T>::iterator Vector<T>::end()
{
    return buffer + size();
}

template<class T>
T& Vector<T>::front()
{
    return buffer[0];
}

template<class T>
T& Vector<T>::back()
{
    return buffer[my_size - 1];
}

template<class T>
void Vector<T>::push_back(const T & v)
{
    if (my_size >= my_capacity)
        reserve(my_capacity +5);
    buffer [my_size++] = v;
}

template<class T>
void Vector<T>::pop_back()
{
    my_size--;
}

template<class T>
void Vector<T>::reserve(unsigned int capacity) {
    T * newBuffer = new T[capacity];
   
    for (unsigned int i = 0; i < my_size; i++)
        newBuffer[i] = buffer[i];
    
    my_capacity = capacity;
    delete[] buffer;
    buffer = newBuffer;
}
template<class T>
unsigned int Vector<T>::size()const//
{
    return my_size;
}

template<class T>
void Vector<T>::resize(unsigned int size)
{
    reserve(size);
    my_size = size;
}

template<class T>
T& Vector<T>::operator[](unsigned int index)
{
    return buffer[index];
}

template<class T>
unsigned int Vector<T>::capacity()const
{
    return my_capacity;
}

template<class T>
Vector<T>::~Vector()
{
    delete[]buffer;
}

#endif 
Last edited on
for (unsigned int i = 0; i , my_size; i++) Here is the problem.
Then how can I improve it?? Regardless of my grade, this assignment drives me mad due to curiosity
Can you find any error from

1
2
3
4
5
6
7
8
9
template<class T>
Vector<T>::Vector(const Vector<T> & v)
{
    my_size = v.my_size;
    my_capacity = v.my_capacity;
    buffer = new T[my_capacity];
    for (unsigned int i = 0; i , my_size; i++)
	buffer[i] = v.buffer[i]; 
}
Look at that line. What does it do? How it works?
Topic archived. No new replies allowed.