c++ - function delete dont work in the second time i activate it -


i have function contains delete in it:

void vector::reserve(int n){     //if there need increase size of vector     if (n > _size){         int* tamparr;         //check new size of array should         _capacity = n + (n - _capacity) % _resizefactor;         tamparr = new int[_capacity];         //put in new array element have in curr array         (int = 0; < _size; i++){             tamparr[i] = _elements[i];         }         delete[] _elements;         _elements = null;//this          //put new array in _element         _elements = new int[n];         (int = 0; < _size; i++){             _elements[i] = tamparr[i];         }         delete[] tamparr;     } } 

the class field is:

private:     //fields     int* _elements;     int _capacity; //total memory allocated     int _size; //size of vector access     int _resizefactor; // how many cells add when need reallocate 

for reason first time use function doesn't show errors , work perfect in second time stops in line: "delete[] _elements;" stops. in addition when run function 1 time stops @ end of object:

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

can me?

you don't check _elements valid before delete it. assuming don't set _elements somewhere else, major bug. add: if(_elements) delete[] _elements; , make sure initialize *_elements null before function called.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -