c++ vector problems -


i working std::vector hold objects have dynamically allocated members, , when go put things vector few things happen not understanding.

  1. i call push_back() , use constructor of objects argument, reason goes destructor of object. why this; should adding not deleting?

  2. i call push_back() second time doing same before, time throws illegal memory access @ dbgdel.cpp operator delete (line 52). delete should never called in constructor, or push_back().

i uncertain sections of code pertinent question lines in question pretty entrenched in method.

edit: code added

class thing{     int** array;     int size;   // of square array     point current; // location     thing(int _n){         // allocates, , gives values array, , members         // constructor     } }; class thingmgr{     thing * control;     thing * current;     thing * previous;     int size;  // size of all. same use in thing     thingmgr(int _n){         size = _n;         control = new thing(size);         current = new thing(size);         previous = new thing(size);     }     void rearrange(int _num){         std::vector<thing> possibles;          // performs deterministic work on members          // [0] first         possibles.push_back(thing(size)); // succeeds         // [1] second         possibles.push_back(thing(size)); // fails          // more operations performed never reached.     } }; 

first: call push_back() , use constructor of objects argument, reason goes destructor of object. why this; should adding not deleting?

you pushing copy of element vector. construct temporal element, copy-constructor called create copy within vector, destructor of temporal element called.

second: call push_back() second time doing same before, time throws illegal memory access @ dbgdel.cpp operator delete (line 52). delete should never called in constructor, or push_back().

it's strange happen on second call, when vector needs regrow copies elements again.

you failing provide proper copy-constructor element in question.


Comments

Popular posts from this blog

delphi - How to convert bitmaps to video? -

jasper reports - Fixed header in Excel using JasperReports -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -