Okay I was trying to get my Dynamic Array to resize when I append but it just seems to break and never increase the capacity that it can store. Anyone able to figure out where my problem is?
#include "DynamicArray.h"
#include <cstring>
#include <iostream>
DynamicArray::DynamicArray()
: m_length(0), m_capacity(0), m_scaling_factor(2.0), m_data(nullptr) {
}
DynamicArray::DynamicArray(double scaling_factor, unsigned int capacity) {
this -> m_scaling_factor = scaling_factor;
this -> m_capacity = capacity;
this -> m_data = new int[m_capacity];
}
DynamicArray::DynamicArray(double scaling_factor, unsigned int length, int default_value) {
this -> m_scaling_factor = default_value;
this -> m_length = default_value;
this -> m_data = new int[m_length];
}
DynamicArray::DynamicArray(const DynamicArray& other) {
// use the assignment operator
(*this) = other;
}
DynamicArray::~DynamicArray() {
delete[] m_data;
}
void DynamicArray::append(int value) {
if(this -> m_length == this -> m_capacity){
this -> m_capacity *= 2;
int *temp = new int[this->m_capacity];
std::memcpy(m_data, temp, sizeof(int) * m_length);
for(unsigned int i = 0; i < this->m_capacity/2; i++){
temp[i] = this->m_data[i];
}
delete[] this->m_data;
this->m_data = temp;
}
this->m_data[this->m_length] = value;
this->m_length++;
}