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++;
}

Here's something to look at. I'm not sure if it fixes your problem, but it appears to be a bug. If the DynamicArray is initializedf with the empty constructor, m_capacity is set to 0. If a value is then appended, the capacity will remain at 0. I would suggest using an actaul value as the default(100 maybe).

Something else I noticed. In one of your constructors, you pass in a scaling factor, but you assign default_value to m_scaling_factor.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.