#ifndef PROJ7_MYVECTOR #define PROJ7_MYVECTOR #include \"proj7-ContainerIfc.h\" template <class T> class MyVector : public ContainerIfc<T> { public: /** * MyVector * * This is the default...

50.1K

Verified Solution

Question

Programming

#ifndef PROJ7_MYVECTOR

#define PROJ7_MYVECTOR

#include \"proj7-ContainerIfc.h\"

template

class MyVector : public ContainerIfc

{

public:

/**

* MyVector

*

* This is the default constructor that sets size equal

* to 0 and capacity to 10.

*

* Parameters: none

*

* Output:

* return: none

* reference parameters: none

* stream: none

*/

MyVector();

/**

* ~MyVector

*

* This is the destructor that deletes memory

*

* Parameters: none

*

* Output:

* return: none

* reference parameters: none

* stream: none

*/

~MyVector();

/**

* MyVector

*

* This is the copy constructor

*

* Parameters:

* v: the object that you want to copy over

*

* Output:

* return: none

* reference parameters: none

* stream: none

*/

MyVector(const MyVector &);

/**

* = operator

*

* This is the overloaded assignment operator

*

* Parameters:

* v: the object that you want to copy over

*

* Output:

* return: none

* reference parameters: none

* stream: none

*/

MyVector &operator=(const MyVector &);

/**

* pushFront

*

* Prepends a value to the array

*

* Parameters:

* e: The value that you want to prepend

*

* Output:

* return: none

* reference parameters: none

* stream: none

*/

MyVector &pushFront(T);

/**

* pushBack

*

* Appends a vlue to the array

*

* Parameters:

* e: The value that you want to append

*

* Output:

* return: none

* reference parameters: none

* stream: none

*/

MyVector &pushBack(T);

/**

* popFront

*

* Removes the first index of the array and shifts all elementsleftward

*

* Parameters:

* e: The value that was removed

*

* Output:

* return: none

* reference parameters: e

* stream: none

*/

MyVector &popFront(T &);

/**

* popBack

*

* Removes the last index of the array

*

* Parameters:

* e: The value that was removed

*

* Output:

* return: none

* reference parameters: none

* stream: none

*/

MyVector &popBack(T &);

/**

* front

*

* Returns the first element of the array

*

* Parameters: none

*

* Output:

* return: Copy of the first data item in the MyVector

* reference parameters: none

* stream: none

*/

T front();

/**

* back

*

* Returns the last element of the array

*

* Parameters: none

*

* Output:

* return: Returns a copy of the last data item in MyVector

* reference parameters: none

* stream: none

*/

T back();

/**

* [] operator

*

* Returns a reference to data element n in MyVector

*

* Parameters:

* n: index of item to return

*

* Output:

* return: Returns a reference to data element n in MyVector

* reference parameters: none

* stream: none

*/

T &operator[](int);

/**

* getSize

*

* Returns size of MyVector array

*

* Parameters: none

*

* Output:

* return: an integer value representing the number of elementsin the list

* reference parameters: none

* stream: none

*/

int getSize();

/**

* isEmpty

*

* Returns state information about the list

*

* Parameters: none

*

* Output:

* return: Returns state information about the list

* reference parameters: none

* stream: none

*/

bool isEmpty();

/**

* erase

*

* Erases a vector

*

* Parameters: none

*

* Output:

* return: none

* reference parameters: none

* stream: none

*/

void erase();

private:

T *data;

int size;

int capacity;

/**

* grow

*

* Increases the capacity of data by doubling the previous valueand allocating

* the appropriate memory for data

*

* Parameters: none

*

* Output:

* return: none

* reference parameters: none

* stream: none

*/

void grow();

/**

* shiftRight

*

* Shifts all values in the array one space to the right

*

* Parameters: none

*

* Output:

* return: none

* reference parameters: none

* stream: none

*/

void shiftRight();

/**

* shiftLeft

*

* Shifts all values in the array one space to the left

*

* Parameters: none

*

* Output:

* return: none

* reference parameters: none

* stream: none

*/

void shiftLeft();

};

template

MyVector::MyVector()

{

this->size = 0;

this->capacity = 10;

this->data = new T[this->capacity];

}

template

MyVector::~MyVector()

{

delete[] this->data;

}

template

MyVector::MyVector(const MyVector &v)

{

this->size = v.size;

this->capacity = v.capacity;

this->data = new T[this->capacity];

// Copy each array item over

for (int i = 0; i < this->size; i++)

{

this->data[i] = v.data[i];

}

}

template

MyVector &MyVector::operator=(constMyVector &v)

{

this->size = v.size;

this->capacity = v.capacity;

this->data = new T[this->capacity];

// Copy each array item over

for (int i = 0; i < this->size; i++)

{

this->data[i] = v.data[i];

}

return *this;

}

template

MyVector &MyVector::pushFront(T e)

{

// Resize if necessary

if (this->size == this->capacity)

{

this->grow();

}

// Shift elements to the right

this->shiftRight();

// Add new value to first index of array

this->data[0] = e;

// Increment size

this->size++;

return *this;

}

template

MyVector &MyVector::pushBack(T e)

{

// Resize if necessary

if (this->size == this->capacity)

{

this->grow();

}

// Add value to array

this->data[this->size] = e;

// Increment size

this->size++;

return *this;

}

template

MyVector &MyVector::popFront(T &e)

{

// Throw BADINDEX if empty

if (this->size <= 0)

{

throw BADINDEX();

}

// Set e equal to the first value

e = this->front();

// Shift elements to the left removing the first index

this->shiftLeft();

// Decrement size

this->size--;

return *this;

}

template

MyVector &MyVector::popBack(T &e)

{

// Throw BADINDEX if empty

if (this->size <= 0)

{

throw BADINDEX();

}

// Set e equal to the last value

e = this->back();

// Remove last element by creating new array and copyingvalues

T *temp = new T[this->capacity];

// Ignore last element and copy all values

for (int i = 0; i < this->size - 1; i++)

{

temp[i] = this->data[i];

}

// Deallocate current array

delete[] this->data;

// Allocate new temp array

this->data = temp;

// Decrement size

this->size--;

return *this;

}

template

T MyVector::front()

{

// Throw BADINDEX if empty

if (this->size <= 0)

{

throw BADINDEX();

}

return this->data[0];

}

template

T MyVector::back()

{

// Throw BADINDEX if empty

if (this->size <= 0)

{

throw BADINDEX();

}

return this->data[this->size - 1];

}

template

T &MyVector::operator[](int n)

{

// Throw BADINDEX if n doesn't exist

if (n > this->size - 1)

{

throw BADINDEX();

}

return this->data[n];

}

template

int MyVector::getSize()

{

return this->size;

}

template

bool MyVector::isEmpty()

{

bool isEmpty = true;

// Check if size is greater than 0

if (this->size > 0)

{

isEmpty = true;

}

return isEmpty;

}

template

void MyVector::erase()

{

// Erase vector by deallocating and allocating a new one

// Reset size & capacity

this->size = 0;

this->capacity = 10;

// Create new empty array

T *temp = new T[this->capacity];

// Delete old array

delete[] this->data;

// Set current array to new array

this->data = temp;

}

template

void MyVector::grow()

{

// Double capacity as instructions say

this->capacity *= 2;

T *temp = new T[this->capacity];

// Copy each array item over

for (int i = 0; i < this->size; i++)

{

temp[i] = this->data[i];

}

// Deallocate current array

delete[] this->data;

// Allocate new temp array

this->data = temp;

}

template

void MyVector::shiftRight()

{

// Create a new array

T *temp = new T[this->capacity];

// Copy values over shifting one to the right

for (int i = 1; i < this->size + 1; i++)

{

temp[i] = this->data[i - 1];

}

// Deallocate current array

delete[] this->data;

// Allocate new temp array

this->data = temp;

}

template

void MyVector::shiftLeft()

{

// Create new array

T *temp = new T[this->capacity];

for (int i = 1; i < this->size; i++)

{

temp[i - 1] = this->data[i];

}

// Deallocate current array

delete[] this->data;

// Allocate new temp array

this->data = temp;

}

#endif

Where is the memory leak located in the abovecode?

Answer & Explanation Solved by verified expert
4.3 Ratings (826 Votes)
Solution Memory Leakage is a condition that occurs whenever a programmer allocates the memory to a variable within the program but forgets to deallocate the memory that is assigned to that variable at the end of the program So this    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students