Copying a dynamic array and deleting the original using an overloaded operator

时间:2017-08-30 20:23:01

标签: c++ arrays object dynamic operator-overloading

I am creating a class that will keep track of students. In this class, I use an overloaded = to copy these student objects. To track their classes, I use a dynamic array. The array copies just fine; however, when clearing the variables of a student object, any object that had copied from it before also has their array wiped. Here is the code:

devicelist

The main offenders almost certainly being either of these two

R.id.listDevicesMain

The output for a program is:

<div class="btn-group d-flex" role="group">
   <a href="" class="btn btn-secondary w-100">Previous</a>
   <a href="" class="btn btn-secondary w-100">Next</a>
</div>

What could I be doing wrong to cause this?

2 个答案:

答案 0 :(得分:0)

You need to create a copy of the class list, or both student objects will have a field that points to the same location in memory.

Freeing one of these fields will cause the other one to also be freed.

In your copy constructor and assignment operator, create a new array of the same size for the new object, and then copy each of the elements to the new object.

答案 1 :(得分:0)

<!--Minimum compatible version required-->
<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.2</version>
    <configuration>
        <jdkToolchain>
            <version>9</version>
        </jdkToolchain>
    </configuration>
</plugin>

The assignment operator should take a <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.6.2</version> <executions> <execution> <id>default-compile</id> <configuration> <!-- compile everything to ensure module-info contains right entries --> <!-- required when JAVA_HOME is JDK 8 or below --> <jdkToolchain> <version>9</version> </jdkToolchain> <release>9</release> </configuration> </execution> <execution> <id>base-compile</id> <goals> <goal>compile</goal> </goals> <!-- recompile everything for target VM except the module-info.java --> <configuration> <excludes> <exclude>module-info.java</exclude> </excludes> </configuration> </execution> </executions> <!-- defaults for compile and testCompile --> <configuration> <!-- jdkToolchain required when JAVA_HOME is JDK 9 or above --> <jdkToolchain> <version>[1.5,9)</version> </jdkToolchain> <source>1.5</source> <target>1.5</target> </configuration> </plugin> reference parameter. The assignment operator should also return a reference, and not a value.

This class is also missing a copy constructor.

In the assignment operator:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.6.2</version>
    <executions>
      <execution>
        <id>default-compile</id>
        <configuration>
          <!-- compile everything to ensure module-info contains right entries -->
          <release>9</release>
        </configuration>
      </execution>
      <execution>
        <id>base-compile</id>
        <goals>
          <goal>compile</goal>
        </goals>
        <!-- recompile everything for target VM except the module-info.java -->
        <configuration>
          <excludes>
            <exclude>module-info.java</exclude>
          </excludes>
        </configuration>
      </execution>
    </executions>
    <!-- defaults for compile and testCompile -->
    <configuration>
      <!-- Only required when JAVA_HOME isn't at least Java 9 and when haven't configured the maven-toolchains-plugin -->
      <jdkToolchain>
        <version>9</version>
      </jdkToolchain>
      <release>6</release>
    </configuration>
</plugin>

This is a plain pointer. For starters, this is a memory leak. The previous pointer (if there is one) gets lost, and its allocated memory gets leaked.

Now you have two instances of your class with the same Student operator =(Student& student) pointer. Consequently, when the const method gets called on one of those classes, it'll be deleted, and since the other instance of the class has the same pointer, it's now pointing to deleted memory.

Your assignment operator must properly delete the existing array, if there is one, and clone the array held in the instance being assigned from, if there is one. Most of this would also apply to the copy constructor that you must implement.

This class is also missing a destructor, which will cause another memory leak.

You have all of these multiple fundamental problems with your class. You must fix all of them in order for your class to work correctly. You must implement a proper copy constructor, assignment operator, and destructor, that correctly clone, copy, and destroy instances of the data held by your class.

It appears that you haven't yet learned how to use containers and smart pointers, and the point of your excersize if for you to learn how to properly keep track of allocated memory, avoid memory corruption and memory leaks. That is obviously an important skill to learn; but once you've figured all of this out, and your class works correctly, you'll simply replace your pointers by a this->classList = student.classList; , and not worry about it any more.