Note that there are some explanatory texts on larger screens.

plurals
  1. POcrash if deleting char* member in destructor of an imbricated own container using vector
    primarykey
    data
    text
    <p>till then i used to cope, here i'm trying to build my own container class using vector. I need it for my work. I'm using <strong>codeblocks 10.05</strong></p> <pre><code>class myclass { public : vector&lt;myclass&gt; array; char * classname; ... </code></pre> <p>The problem is that my datum of my class is <strong>well displayed on screen if its deleting doesn't occur in the destructor.</strong> My function show() diplays odd characters if I delete classname. I suppose it to come from my method to build object and a matter of range when I pass them as argument.</p> <pre><code> myclass::~myclass() { //if(classname) delete [] classname; } </code></pre> <p>And this is how it is initialized in <strong>the constructor :</strong></p> <pre><code> myclass::myclass(long lvl = 0, const char name[] = "undefined") :ID(++ ID_counter) { level = lvl; int namelength = strlen(name); classname = new char[namelength + 1]; strcpy(classname, name); } </code></pre> <p><strong>add_content(const myclass &amp; c)</strong> is supposed to make a copy of teh elements of c.array and "push_back" them in this->array I find out per chance that we could put an object with no name as parameter : <strong>mycontainer.add_content(myclass(3,5));</strong> it works but i'm skeptic about the scope it should have</p> <pre><code>&gt; int main() &gt; { &gt; myclass mycontainer(0); &gt; mycontainer.add_content(myclass(3,5)); &gt; ... </code></pre> <p>Here is <strong>the full code :</strong></p> <pre><code>#include &lt;vector&gt; #include &lt;iostream&gt; using namespace std; class myclass { public : vector&lt;myclass&gt; array; static long ID_counter; long ID; long level; char * classname; myclass(int n, long lvl, const char name[]); //push_back n elements, lvl = 0, name = "undefined" myclass(long lvl, const char name[]); //lvl = 0, name = "undefined" myclass::~myclass(); void set_level(long lvl); //recursive function, go down the tree void add(int n); //push_back n elements void add(const myclass &amp; c); //push_back and set back the levels void add_content(const myclass &amp; c); //push_back all the c.array[i] and set back the levels void show(); template &lt;typename T&gt; myclass &amp; operator[](const T it){ return array[it]; } }; long myclass::ID_counter = 0; myclass::myclass(long lvl = 0, const char name[] = "undefined") :ID(++ ID_counter) { level = lvl; int namelength = strlen(name); classname = new char[namelength + 1]; strcpy(classname, name); } myclass::myclass(int n, long lvl, const char name[] = "undefined") :ID(++ ID_counter) { level = lvl; int namelength = strlen(name); classname = new char[namelength + 1]; strcpy(classname, name); for(int i = 0; i &lt; n; i++) array.push_back( myclass(this-&gt;level + 1) ); } myclass::~myclass() { //if(classname) delete [] classname; //there is the point ! } void myclass::add(int n = 1) { for(int i = 0; i &lt; n; i++) array.push_back( myclass(level + 1) ); } void myclass::add(const myclass &amp; c) { array.push_back(c); array[array.size() - 1].set_level(level + 1); } void myclass::add_content(const myclass &amp; c) { for(int i = 0; i &lt; c.array.size(); i++) { array.push_back(c.array[i]); array[array.size() - 1].set_level(level + 1); } } void myclass::set_level(long lvl) { level = lvl; for(int i = 0; i &lt; array.size(); i++) array[i].set_level(level + 1); } void myclass::show() { cout &lt;&lt; "ID : " &lt;&lt; ID &lt;&lt; "\tLvl : " &lt;&lt; level &lt;&lt; "\t Classname : " &lt;&lt; classname &lt;&lt; endl; } int main() { myclass mycontainer(0); //ground level mycontainer.add_content(myclass(3,5)); //the 3 elements level 5 should be reset to 0+1 mycontainer.show(); for(int i = 0; i &lt; mycontainer.array.size(); i++) { mycontainer[i].show(); for(int j= 0; j &lt; mycontainer[i].array.size(); j++) mycontainer[i][j].show(); } cout &lt;&lt; "\ncheckpoint\n"; system("PAUSE"); //press any key to crash! return 0; } </code></pre> <p>here <strong>no deletion of *classname in the destructor</strong>, the program rend this :</p> <pre><code>ID : 1 Lvl : 0 Classname : undefined ID : 3 Lvl : 1 Classname : undefined ID : 4 Lvl : 1 Classname : undefined ID : 5 Lvl : 1 Classname : undefined checkpoint Appuyez sur une touche pour continuer... </code></pre> <p><strong>with deletion</strong> and some bad characters show up with a crash:</p> <pre><code>ID : 1 Lvl : 0 Classname : undefined ID : 3 Lvl : 1 Classname : ░(? ID : 4 Lvl : 1 Classname : ░(? ID : 5 Lvl : 1 Classname : ░(? checkpoint Appuyez sur une touche pour continuer... Process returned -1073741819 (0xC0000005) execution time : 29.651 s Press any key to continue. </code></pre> <p>I tried to study back some principles about arguments and pointers but this language is too empirical and makes no sense. Thank you for helping.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload