Note that there are some explanatory texts on larger screens.

plurals
  1. POConstructors, destructors and pointers (and vectors and arrays and delete and that)
    primarykey
    data
    text
    <p>Funky title, but honestly I couldn't think of anyone better, sorry :(</p> <p>While experimenting with pointers I came across this and I need help understanding it. Basically, I create a vector of a pointer to an object. When deleting the pointer that's in the vector, I expect the original ovject to be deleted as well. They are one and the same no?</p> <p>Here's what I think I do in the code that follows I create a dynamically allocated array, I then put the pointer to half of the elements of the array in a vector of pointers. Each step of the way every object of the class testDestructor knows which 'order' it was created in, they all get assigned an incrementing number via a static integer.</p> <p>I then pop_back and delete all the pointers in the vector. I only use pop_back because I wanted to see if they get deleted by the vector class, apperantly they don't, but idk if I'm missing something there. The important bit is I delete it.</p> <p>Now, what I expect to happen is that this deletes the corresponding elements of the array as well. This is what SHOULD happen, because the vector and the array element point to the same place in the memory. ie, the destructor should get called.</p> <p>So when I then delete the array, I expect that either only 5 of the elements get deleted, or a run-time error occurs (I've had this happen before when I try deleting pointers that already are deleted, but that might have been a different scenario idk). Namely, I expect that only the destructor should only be called five times.</p> <p>HOWEVER, that's not what happens. The constructor gets called 10 times but the destructor gets called 15 times. What am I missing? Am I missing a constructor (I only know about default constructors and copy constructors, are there more), or is it something else? Because, in my mind, when the destructor is gone the object is gone.</p> <p>Sorry if it's too much code:</p> <p>testDestructor.h:</p> <pre><code>#ifndef TESTDESTRUCTOR_H #define TESTDESTRUCTOR_H class testDestructor { public: testDestructor(); testDestructor(const testDestructor &amp;); ~testDestructor(); static int maxTest; int test; }; #endif </code></pre> <p>testDestructor.cpp:</p> <pre><code>#include "testDestructor.h" #include &lt;iostream&gt; using namespace std; int testDestructor::maxTest = 0; testDestructor::testDestructor() { test = maxTest++; cout &lt;&lt; "testDestructor nr " &lt;&lt; test &lt;&lt; " created successfully\n"; } testDestructor::testDestructor(const testDestructor &amp;destructorToCopy) { test = maxTest++; cout&lt;&lt; "testDestructor nr " &lt;&lt; test &lt;&lt; " created successfully (using the copy constructor\n"; } testDestructor::~testDestructor() { //maxTest--; cout &lt;&lt; "testDestructor " &lt;&lt; test &lt;&lt; " destroyed successfully\n"; } </code></pre> <p>main.cpp:</p> <pre><code>#include &lt;iostream&gt; #include "testDestructor.h" #include &lt;vector&gt; using namespace std; int main() { cout &lt;&lt; " creating pointer array:\n\n"; testDestructor *testPtr = new testDestructor[10]; cout &lt;&lt; " intitiating vector\n\n"; vector&lt;testDestructor*&gt; testVct; for (int i = 0; i &lt; 5; i++) { cout &lt;&lt; " pushing back vector " &lt;&lt; i &lt;&lt; ":\n"; testVct.push_back(testPtr + i); cout &lt;&lt; "testDestructor " &lt;&lt; testVct[i]-&gt;test &lt;&lt; " pushed back\n"; } cout &lt;&lt; "\n"; for (int i = 4; i &gt;= 0; i--) { cout &lt;&lt; " popping back vector " &lt;&lt; i &lt;&lt; ":\n"; cout &lt;&lt; "testDestructor " &lt;&lt; testVct[i]-&gt;test &lt;&lt; " popped back\n"; delete testVct[i]; testVct.pop_back(); } cout &lt;&lt; "\n"; cout &lt;&lt; " deleting pointer array\n\n"; delete [] testPtr; } </code></pre>
    singulars
    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