Help with the implementation. I tried to create a class with a dynamic array. so that you can add items to the end.
class DynArray { private: int size; double *mass; public: DynArray() : size(0), mass(0) {} void add(const double &num) { double *p=new double[size+1]; memcpy(p,mass,sizeof(double)*size); p[size]=num; delete[] mass; mass=p; size++; } double get(const int &n) { return mass[n]; } ~DynArray(){delete[] mass;} };
This code is completely working, I'm wondering if there is any memory leak anywhere?
p
array, since size you increased by 1 - Mikedouble *p=new double[size+1]; memcpy(p,mass,sizeof(double)*size); p[size]=num; delete[] mass; mass=p; size++;
double *p=new double[size+1]; memcpy(p,mass,sizeof(double)*size); p[size]=num; delete[] mass; mass=p; size++;
as it is easier and shorter ... - Mikemass
top
- Mike