For some reason my implementation allows 0 size array access to 0 position and 5 size array access 50 position. While both positions must get me an segmentation fault error for my perspective. I think that maybe it can be caused by allocate memory wrongly but by my side it looks right ,someone can tell me why I obtain that result.
My code for array:
class vector{
private:
int length;
int cap;
T* arr;
public:
vector(int n){
arr = new T[n];
length = n;
cap = n;
}
vector():vector(0){};
vector(vector<T> & other){
arr = new T[other.size()];
length = other.size();
cap = other.capacity();
}
~vector(){
delete[] arr;
}
T& operator[](int ind) {
return arr[ind];
}
const T& operator[](int ind) const{
return arr[ind];
}
};
My test:
int main(int argc, char* argv[])
{
vector<int> v;
v[0] = 5;
vector<int> v2(5);
std::cout<<v2[50]<<" "<<v[0]<<std::endl;
}
Output:
0 0
Aucun commentaire:
Enregistrer un commentaire