Question

In: Computer Science

#ifndef PROJ7_MYVECTOR #define PROJ7_MYVECTOR #include "proj7-ContainerIfc.h" template <class T> class MyVector : public ContainerIfc<T> { public:...

#ifndef PROJ7_MYVECTOR

#define PROJ7_MYVECTOR

#include "proj7-ContainerIfc.h"

template <class T>

class MyVector : public ContainerIfc<T>

{

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<T> &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<T> &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<T> &pushBack(T);

/**

* popFront

*

* Removes the first index of the array and shifts all elements leftward

*

* Parameters:

* e: The value that was removed

*

* Output:

* return: none

* reference parameters: e

* stream: none

*/

MyVector<T> &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<T> &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 elements in 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 value and 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 <class T>

MyVector<T>::MyVector()

{

this->size = 0;

this->capacity = 10;

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

}

template <class T>

MyVector<T>::~MyVector()

{

delete[] this->data;

}

template <class T>

MyVector<T>::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 <class T>

MyVector<T> &MyVector<T>::operator=(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];

}

return *this;

}

template <class T>

MyVector<T> &MyVector<T>::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 <class T>

MyVector<T> &MyVector<T>::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 <class T>

MyVector<T> &MyVector<T>::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 <class T>

MyVector<T> &MyVector<T>::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 copying values

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 <class T>

T MyVector<T>::front()

{

// Throw BADINDEX if empty

if (this->size <= 0)

{

throw BADINDEX();

}

return this->data[0];

}

template <class T>

T MyVector<T>::back()

{

// Throw BADINDEX if empty

if (this->size <= 0)

{

throw BADINDEX();

}

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

}

template <class T>

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

{

// Throw BADINDEX if n doesn't exist

if (n > this->size - 1)

{

throw BADINDEX();

}

return this->data[n];

}

template <class T>

int MyVector<T>::getSize()

{

return this->size;

}

template <class T>

bool MyVector<T>::isEmpty()

{

bool isEmpty = true;

// Check if size is greater than 0

if (this->size > 0)

{

isEmpty = true;

}

return isEmpty;

}

template <class T>

void MyVector<T>::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 <class T>

void MyVector<T>::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 <class T>

void MyVector<T>::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 <class T>

void MyVector<T>::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 above code?

Solutions

Expert Solution

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 memory is actually kept occupied by the variable even when the program has actually finished its execution and hence it cannot be used by some other variable, this phenomenon is known as memory leakage.

In the program code that is given above, the variable temp in method void MyVector<T>::shiftLeft(), is actually allocated the memory but this variable is never deallocated by the programmer due to which it would keep the memory assigned to it for an indefinite amount of time and hence this would lead to the memory leakage.

void MyVector<T>::shiftLeft()

{

// Create new array

T *temp = new T[this->capacity]; //Would lead to memory leakage as the variable is allocated the memory but not deallocated

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;

}

Here's the solution to your question and it is absolutely correct, please please please provide it a 100% rating. Thanks for asking and happy learning!!


Related Solutions

#ifndef CCALC_HEADER #define CCALC_HEADER    class   CCalc { public:     // member functions     CCalc();     void    Add(double...
#ifndef CCALC_HEADER #define CCALC_HEADER    class   CCalc { public:     // member functions     CCalc();     void    Add(double value);     void    Clear();     void    Divide(double value);     double  GetValue() const;     void    Multiply(double value);     void    SetValue(double  newValue);     void    Subtract(double value);    private:     // data members     double  m_total; };    #endif // CCALC_HEADER int     main() {     CCalc       calculator;     char        choice;        // loop and let the user manipulate the calculator     do {         // display the menu and get the user selection         DisplayMenu();         cout <<...
//BEGIN--TABLE.H-- #ifndef TABLE_H #define TABLE_H #include        // Provide string #include // Provide hash #include...
//BEGIN--TABLE.H-- #ifndef TABLE_H #define TABLE_H #include        // Provide string #include // Provide hash #include        // Provide list using namespace std; class Table { public:    // MEMBER CONSTANT    static const unsigned int TABLE_SIZE = 13;    Table() { total_records = 0; }    virtual ~Table() { }    unsigned int get_total() { return total_records; }    virtual void insert(string key) =0;    virtual void print() =0;    protected:    unsigned int total_records;    // HELPER...
#ifndef CONTAINER_H #define CONTAINER_H using namespace std; class container { public: // CONSTRUCTOR container(); ~container(); container(const...
#ifndef CONTAINER_H #define CONTAINER_H using namespace std; class container { public: // CONSTRUCTOR container(); ~container(); container(const container& c); container& operator=(const container& c) // MEMBER FUNCTIONS bool lookup(const int& target); void remove(const int& target); void add(const int& target); private: struct node{ int key; node *next; }; node* head; node* tail; }; #endif For a linked list based implementation of a container class shown above, implement the constructor, copy constructor, destructor and copy assignment functions.
I create a h file, and I wrote #ifndef MYSTACK_H #define MYSTACK_H #include <cstdlib> #include <cstddef>...
I create a h file, and I wrote #ifndef MYSTACK_H #define MYSTACK_H #include <cstdlib> #include <cstddef> #include <iostream> struct node { int value; node* next; node(int value, node* next = nullptr) { this->value = value; this->next = next; } }; class mystack { private: node* stack_top; size_t stack_size; public: mystack(); mystack(const mystack& x); ~mystack(); mystack& operator=(const mystack& x); size_t size() const; bool empty() const; void clear(); const int& top() const; void push(int value); void pop(); void clone(const mystack& x); };...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; template <class T> double half(int x)...
Debug please. It's in C++ #include<iostream> #include<string> using namespace std; template <class T> double half(int x) { double h = x / 2; return h; } class TuitionBill { friend ostream& operator<<(ostream, TuitionBill); private: string student; double amount; public: TuitionBill(string, double); double operator/(int); }; TuitionBill::TuitionBill(string student, double amt) { student = student; amount = amt; } double TuitionBill::operator/(int factor) { double half = amount / factor; return hafl; } ostream& operator<<(ostream& o, TuitionBill) { o << t.student << " Tuition:...
#include <stdio.h> #include <cmath> #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #pragma warning (disable : 4996) int...
#include <stdio.h> #include <cmath> #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #pragma warning (disable : 4996) int main() {    const char* filename = "samples.coe"; const int N = 1024; FILE* file = fopen(filename, "w"); if (file == NULL) { perror("fopen"); } fprintf(file, "; These are 1024 sample values in range -1 to 1,\n"); fprintf(file, "; Sine Wave 0\n"); fprintf(file, "memory_initialization_radix = 10;\n"); fprintf(file, "memory_initialization_vector\n"); double values[N]; double delta = M_PI / (N - 1); for (int i = 0; i...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std;...
implement c++ Quicksort using median of 3 #ifndef QSORT_H #define QSORT_H #include #include using namespace std; template T median_of_three(T& a, T& b, T& c, TComparator comparator) { } template size_t partition(vector& vec, TComparator& comparator, size_t low, size_t high) { // TODO: implement. } template void QuickSort(vector& vec, TComparator comparator,size_t low,size_t high) { if(comparator(low,high)){ size_t loc = partition(vec,comparator,low,high); QuickSort(vec,comparator,low,loc-1); QuickSort(vec,comparator,loc+1,high); } return; } template void quicksort(vector& vec, TComparator comparator) { // TODO: implement. size_t size = vec.size(); QuickSort(vec,comparator,0,size-1); } #endif test_case:...
Modify the SimpleVector by doubling the arraysize limit. --------SimpleVector.h------- #ifndef SIMPLEVECTOR_H #define SIMPLEVECTOR_H // SimpleVector class...
Modify the SimpleVector by doubling the arraysize limit. --------SimpleVector.h------- #ifndef SIMPLEVECTOR_H #define SIMPLEVECTOR_H // SimpleVector class template #include #include // Needed for bad_alloc exception #include // Needed for the exit function using namespace std; template class SimpleVector { private: T *aptr; // To point to the allocated array int arraySize; // Number of elements in the array void memError(); // Handles memory allocation errors void subError(); // Handles subscripts out of range public: // Default constructor SimpleVector() { aptr =...
public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get;...
public class Node<T> { Public T Item { get; set; } Public Node<T> Next; { get; set; } public Node (T item, Node<T> next) { … } } public class Polynomial { // A reference to the first node of a singly linked list private Node<Term> front; // Creates the polynomial 0 public Polynomial ( ) { } // Inserts term t into the current polynomial in its proper order // If a term with the same exponent already exists...
C++ existing code #include "ArrayBag.hpp" #include <iostream> /****************************************************** Public Methods *****************************************************/ /* Default Constructor */ template...
C++ existing code #include "ArrayBag.hpp" #include <iostream> /****************************************************** Public Methods *****************************************************/ /* Default Constructor */ template <typename ItemType> ArrayBag<ItemType>::ArrayBag() : item_count_(0) { // initializer list } // end default constructor template <typename ItemType> int ArrayBag<ItemType>::getCurrentSize() const { return item_count_; } template <typename ItemType> bool ArrayBag<ItemType>::isEmpty() const { return item_count_ == 0; } template <typename ItemType> bool ArrayBag<ItemType>::add(const ItemType &new_entry) {    bool has_room_to_add = (item_count_ < DEFAULT_CAPACITY); if (has_room_to_add) { items_[item_count_] = new_entry; item_count_++; } // end if return has_room_to_add;...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT