Note that there are some explanatory texts on larger screens.

plurals
  1. POPossible logical flaw in a C++ test example
    primarykey
    data
    text
    <p>At my university, there is a practical programming test in C++ - and I'm stuck with an example where I am unsure about wheter or not the task in question is even valid and possible to complete correctly.</p> <p><strong>The (simple) tasks:</strong></p> <ul> <li><p>Complete the destructor of <code>Person</code>, so that the allocated <code>name</code> is freed again</p></li> <li><p>In the main function, replace <code>//???</code> with the statement required to free the previously allocated memory</p></li> </ul> <p>At first, the tasks seemed trivial for me: For the destructor, simply write <code>delete[] name</code> and in the main function, use <code>delete[] friends</code>. Presumably, that is also what the author of this example meant us to do.</p> <p><strong>However:</strong></p> <p>There seems to be a flaw in this code example, which causes memory leaks as well as destructors to be called more than once.</p> <p>The person class has no assignment <code>operator =</code>, meaning that as the existing Person objects such as <code>maria</code> are assigned to slots in the <code>friends</code> array in the main function, the internal allocated <code>name</code>s are not copied. So two objects now share the same internal <code>char*</code> pointer! Moreover, the pointer to the name of the <code>Person</code> previously residing in the said array slot is permanentely lost, leading to an unavoidable memory leak.</p> <p>As <code>delete[] friends;</code> is called - the objects in the array are destroyed - leading to their destructors being called and freeing their <code>name</code> members. When the program ends, though, the local Person objects in the scope of <code>main</code> are destructed - which of course have their <code>name</code> members still pointing to memory which was already freed before.</p> <p><strong>The actual question:</strong></p> <ul> <li>Is this test example flawed, or am I missing something?</li> <li>Can the issues listed above be fixed if sticking fully to carrying out the given tasks (altering just the implementation of the destructor, and inserting new code at the commented part in the main function)?</li> </ul> <p>..</p> <pre><code>#include &lt;iostream&gt; using namespace std; int strlen(const char *str) { if (str==0) return 0; int i=0; for (; str[i]; ++i); return i; } void strcpy(const char *src, char *dest) { if (src==0 || dest==0) return; int i=0; for (; src[i]; ++i) dest[i]=src[i]; dest[i]=’\0’; } class Person { char *name; public: Person(const char *str = "Susi") { name = new char[strlen(str)+1]; strcpy(str,name); } Person(const Person &amp;p) { name = new char[strlen(p.name)+1]; strcpy(p.name,name); } ~Person() { //... } void change() { name[4]='e'; } ostream &amp;print(ostream &amp;o) const { o&lt;&lt;name; return o; } }; int main() { Person maria("Maria"), peter("Peter"), franz("Franz"), luisa("Luisa"); Person mary(maria); Person luise; Person p(luise); Person *friends= new Person[7]; friends[0]=maria; friends[1]=peter; friends[2]=franz; friends[3]=luisa; friends[4]=mary; friends[5]=luise; friends[6]=p; friends[5]=luisa; friends[3].change(); friends[4].change(); for (int i=0; i&lt;7; ++i) { friends[i].print(cout); cout&lt;&lt;endl; } //??? return 0; } </code></pre>
    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