Coded-Blood Junior
Coded-Blood Junior

Reputation: 17

Doesn't work Copy Assignment Operator

In my code, I am having trouble on my copy assignment operator. When I try to execute "=" operator in my main(), the content of my source array (numArr) is not copied to my destenation array (numArr2).

Then, for my doubleCap() function, I am try to create a bigger array of double size once my original array is full. However, if I insert delete[] newArr, the compiler will output some random numbers in my array.

This is my code in .cpp

#include <iostream>
#include "NumList.h"

using namespace std;

//Default Constructor
NumList::NumList()
{
    //Actual elements stored in the array
    size = 0;
    //Max capacity of the array
    capacity = 1;
    numList = new int[capacity];
}

//Destructor
NumList::~NumList()
{
    delete[] numList;
}

//Copy Constructor
NumList::NumList(const NumList& anotherNumList)
{
    capacity = anotherNumList.capacity;
    size = anotherNumList.size;
    numList = new int[capacity];
    for (int i = 0; i < size; ++i)
    {
        numList[i] = anotherNumList.numList[i];
    }
}

//Copy Assignment Operator
NumList& NumList::operator= (const NumList& anotherNumList)
{
    //Check if it is self-assigning
    if (this == &anotherNumList)
        return *this;

    //Get rid of the old data
    delete[] numList;

    this->capacity = anotherNumList.capacity;

    //Create and copy to the new array
    numList = new int[capacity];

    for (int i = 0; i < anotherNumList.size; ++i)
    {
        numList[i] = anotherNumList.numList[i];
    }

    return *this;
}

void NumList::print()
{
    for (int i = 0; i < size; ++i)
    {
        cout << numList[i] << " ";
    }
    cout << endl;
}

void NumList::doubleCap()
{
    capacity = capacity * 2;
    //Create a new array when the capacity is full
    int * newArr = new int[capacity];

    for (int i = 0; i < size; ++i)
    {
        newArr[i] = numList[i];

    }
    //Let numlist points to the new array
    numList = newArr;

    //delete[] newArr; <-- 
}

void NumList::insertEnd(int val)
{
    //Double the capacity of the list
    if (size == capacity)
    {
        doubleCap();
    }
    numList[size] = val;
    ++size;
}

void NumList::insertAt(int val, int index)
{
    if (index < 0 || index > capacity)
    {
        cout << "The index is out of range." << endl;
    }
    else
    {
        //Double the capacity of the list
        if (size == capacity)
        {
            doubleCap();
        }
        for (int i = (size-1); i >= index; i--)
        {
            numList[i + 1] = numList[i];
        }
        numList[index] = val;
        ++size;
    }
}

This is my code in main

#include <iostream>
#include <string>
#include <algorithm>
#include "NumList.h"

using namespace std;

int main()
{
    NumList numArr;
    NumList numArr2;

    numArr.insertEnd(10);
    cout << "List 1 after inserting 10: ";
    numArr.print();

    numArr2 = numArr;
    NumList numArr3(numArr);

    numArr.insertEnd(11);
    numArr.insertEnd(12);
    numArr.insertAt(5, 0);
    cout << "List 1: ";
    numArr.print();

    cout << "\nPrint the list 2 of int: ";
    numArr2.print();

    cout << "\nPrint the list 3 of int: ";
    numArr3.print();

    system("pause");
    return 0;

}

Output without line "delete[] newArr;",

List 1 after inserting 10: 10 
List 1: 5 10 11 12

Print the list 2 of int: 

Print the list 2 of int: 10 
Press any key to continue . . . 

Output with line "delete[] newArr;",

List 1 after inserting 10: 10 
List 1: 5 -572662307 -572662307 12

Print the list 2 of int: 

Print the list 2 of int: 10 
Press any key to continue . . . 

Upvotes: 0

Views: 205

Answers (1)

NathanOliver
NathanOliver

Reputation: 180500

You are trying to delete the wrong thing. In doubleCap() you have 2 arrays, the member and the new on you create that has more space. The one that has more space is the one you want to keep so you can't delete it. What you need to do is delete the original array and then assign to it the new one. That makes the function look like

void NumList::doubleCap()
{
    capacity = capacity * 2;
    //Create a new array when the capacity is full
    int * newArr = new int[capacity];

    for (int i = 0; i < size; ++i)
    {
        newArr[i] = numList[i];

    }

    delete [] numList; // get rid of the old array
    //Let numlist points to the new array
    numList = newArr;
}

You are also missing the assignment of anotherNumList.size to this->size in your operator =. That causes the copied list to have the wrong size after assignment.

Upvotes: 4

Related Questions