c++ copy assignment operator classes - c++

I have a problem with the operator = in my class. This is the semplified code:
class:
#include <iostream>
using namespace std;
class Mat{
private:
int *m1;
int *m2;
unsigned rows;
unsigned cols;
void write(unsigned r_max, unsigned c_max){
m1 = new int [r_max*c_max];
for(unsigned i = 0; i < r_max*c_max; i++){
m1[i] = i;
}
}
public:
Mat():
rows(1), cols(1){
m1 = new int [1];
m1[0] = 0;
m2 = new int [1];
m2[0] = 0;
}
Mat(unsigned r, unsigned c):
rows(r), cols(c){
write(rows, cols);
m2 = new int [rows*cols];
for(unsigned i = 0; i < rows*cols; i++)
m2[i] = 0;
}
Mat &operator=(const Mat &w){
int *new_ptr1 = NULL;
new_ptr1 = new int [w.rows*w.cols];
int *new_ptr2 = NULL;
new_ptr2 = new int [w.rows*w.cols];
for(unsigned i = 0; i < w.rows*w.cols; i++){
new_ptr1[i] = w.say_m1(i);
new_ptr2[i] = w.say_m2(i);
}
delete[] w.m1;
delete[] w.m2;
m1 = new_ptr1;
m2 = new_ptr2;
rows = w.rows;
cols = w.cols;
return *this;
}
int say_m1(unsigned i) const{ return m1[i]; }
int say_m2(unsigned i) const{ return m2[i]; }
~Mat(){
delete[] m1;
delete[] m2;
}
};
Here is what I need to do in the main:
#include "Mat.cpp"
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char **argv){
Mat a;
Mat b(20, 20);
a = b;
return 0;
}
I think the problem is in the class where I overload the operator =
I don't know how to copy the right object data into the left object data, I'm not sure also if I delete correctly...

I think the problem is in the class where I overload the operator = I don't know how to copy the right object data into the left object data, I'm not sure also if I delete correctly...
You do not (delete correctly).
Operator = should alter the values of *this. Instead, you delete w.m1 and w.m2 (where w is your parameter).
Corrected code:
Mat &operator=(const Mat &w){
int *new_ptr1 = NULL;
new_ptr1 = new int [w.rows*w.cols];
int *new_ptr2 = NULL;
new_ptr2 = new int [w.rows*w.cols];
for(unsigned i = 0; i < w.rows*w.cols; i++){
new_ptr1[i] = w.say_m1(i);
new_ptr2[i] = w.say_m2(i);
}
delete[] m1; // <<< HERE
delete[] m2; // <<< HERE
m1 = new_ptr1;
m2 = new_ptr2;
rows = w.rows;
cols = w.cols;
return *this;
}
That said, there's a host of other problems with your code:
Assuming you wrote this code to learn how to manipulate dynamic arrays within a class:
you're using a class that manages two different allocated resources directly. You should consider using a smart pointer on m1 and m2 (for exception safety).
Implement the rule of three: constructor (you already have this), copy constructor (you don't have this) and assignment operator (you already have this).
Add a destructor to your class.
** Assuming this is code you will use and did not write for learning arrays:**
[] use std::vectors for m1 and m2 instead. This will save you from having to implement the rule of three and destructor.
You use using namespace std; globally. Don't do that as it creates many many problems.
Edit:
Better code:
#include <iostream>
#include <vector>
class Mat{
std::vector<int> life;
std::vector<int> neighborhood;
public:
// this is unnecessary
// void write(unsigned r_max, unsigned c_max); {
Mat() : life(1), meighborhood(1) // fill each with 1 int with default value (0)
{
}
Mat(unsigned r, unsigned c)
: life(r), meighborhood(c) // fill each with r(and c) ints with default value (0)
{
}
// ~Mat() became unnecessary: destructors of std::vector will deallocate fine
Mat &operator=(const Mat &w) {
// create replacements before doing changing any value
// this way, if you get an exception while creating the data
// the value in the obhect does not change
std::vector<int> new_life(w.life);
std::vector<int> new_neighborhood(w.neighborhood);
life.swap(new_life);
neighborhood.swap(new_neighborhood);
return *this;
}
// use std::vector<int>::at which throws an exception if the index is invalid
// if you are not interested in the validation of the index
// return life[i] and neighborhood[i]
int say_m1(unsigned i) const{ return life.at(i); }
int say_m2(unsigned i) const{ return neighborhood.at(i); }
};

Related

Assignment and retrieval using subscript and equals operator overloads

I've been having a hell of a time trying to solve this. What I am trying to do is use operator overloading so that my objects behave more like a multi dimensional array. I've found solutions to several of the smaller problems involved in making this happen but whenever I try to put it all together there is one issue or another, either lvalue assignment error or invalid initialization from rvalue or just straight up seg fault. I would appreciate any advice TY.
#include <iostream>
#include <vector>
#include <string>
class Matrix {
std::string **m;
public:
Matrix(int x, int y) {
m = new std::string*[x];
for (int i = 0; i < x; i++)
m[x] = new std::string[y];
}
class Proxy {
std::string *mm;
int lastIndex = 0;
public:
Proxy(std::string *s) : mm(s) {}
std::string &operator[](int index) {
lastIndex = index;
return mm[index];
}
std::string &operator=(std::string s) {
mm[lastIndex] = s;
return mm[lastIndex];
}
};
Proxy operator[](int index) {
return Proxy(m[index]);
}
};
int main()
{
Matrix *m = new Matrix(5, 5);
m[2][2] = std::string("It Works");
std::cout << m[2][2] << std::endl;
return 0;
In main(), m is a pointer to a Matrix object, so you need to dereference the pointer in order to access the Matrix object so you can invoke your Matrix::operator[] on it, eg:
int main()
{
Matrix *m = new Matrix(5, 5);
(*m)[2][2] = "It Works";
std::cout << (*m)[2][2] << std::endl;
delete m;
return 0;
}
Online Demo
Otherwise, the pointer is not really needed in your example to begin with, eg:
int main()
{
Matrix m(5, 5);
m[2][2] = "It Works";
std::cout << m[2][2] << std::endl;
return 0;
}
Online Demo
Either way, your Proxy does not need to implement operator= at all, eg:
class Proxy {
std::string *mm;
public:
Proxy(std::string *s) : mm(s) {}
std::string& operator[](int index) {
return mm[index];
}
};
A statement like m[2][2] = "..."; will not invoke your Proxy::operator=, it will invoke only Proxy::operator[]. A statement like m[2] = "..."; would be needed to invoke Proxy::operator=, which doesn't make sense to do in a multi-dimensional scenario.
Also, your Matrix constructor has a bug - writing to m[x] is going out of bounds of the m[] array, so the array is not actually filled at all, and you are corrupting surrounding memory, and leaking memory. You need to write to m[i] instead:
//m[x] = new std::string[y];
m[i] = new std::string[y];
After fixing that, Matrix is still leaking memory, as it does not implement a destructor to free the std::strings. You must delete[] anything you new[] (same with delete and new).
And then, you should finish off implementing support for the Rule of 3/5/0, by implementing a copy constructor and a copy assignment operator (your example code does not need them, but production code should always have them), eg:
#include <iostream>
#include <string>
#include <utility>
class Matrix {
std::string **m;
int m_x, m_y;
public:
Matrix(int x = 0, int y = 0) : m_x(x), m_y(y) {
m = new std::string*[x];
for (int i = 0; i < x; ++i)
m[i] = new std::string[y];
}
Matrix(const Matrix &src) : m_x(src.m_x), m_y(src.m_y) {
m = new std::string*[m_x];
for (int i = 0; i < m_x; ++i) {
m[i] = new std::string[m_y];
for (int j = 0; j < m_y; ++j) {
m[i][j] = src.m[i][j];
}
}
}
~Matrix() {
for (int i = 0; i < m_x; ++i)
delete[] m[i];
delete[] m;
}
Matrix& operator=(const Matrix &rhs) {
if (&rhs != this) {
Matrix temp(rhs);
std::swap(m, temp.m);
std::swap(m_x, temp.m_x);
std::swap(m_y, temp.m_y);
}
return *this;
}
class Proxy {
std::string *mm;
public:
Proxy(std::string *s) : mm(s) {}
std::string& operator[](int index) {
return mm[index];
}
};
Proxy operator[](int index) {
return Proxy(m[index]);
}
};
int main()
{
Matrix m(5, 5);
m[2][2] = "It Works";
std::cout << m[2][2] << std::endl;
Matrix m2(m);
std::cout << m2[2][2] << std::endl;
Matrix m3;
m3 = m2;
std::cout << m3[2][2] << std::endl;
return 0;
}
Online Demo
However, rather than using new[] manually, consider using std::vector instead (which you are already aware of, since you have #include <vector> in your code). This way, the Rule of 3/5/0 can be handled entirely by the compiler for you. std::vector and std::string are both fully compliant with the Rule, and so any compiler-generated destructor, copy constructor, and copy-assignment operator in Matrix will suffice, eg:
#include <iostream>
#include <vector>
#include <string>
class Matrix {
std::vector<std::vector<std::string>> m;
public:
Matrix(int x = 0, int y = 0) {
m.resize(x);
for (int i = 0; i < x; ++i)
m[i].resize(y);
}
class Proxy {
std::vector<std::string> &mm;
public:
Proxy(std::vector<std::string> &s) : mm(s) {}
std::string& operator[](int index) {
return mm[index];
}
};
Proxy operator[](int index) {
return Proxy(m[index]);
}
};
Online Demo

How to set an array in a class in C++

I'd like to store an array of numbers in a private variable of a class using a setter method, but am unsure how to.
The program requires a default constructor, and the other essential methods but for simplicity i've only provided the default constructor.
class numberList{
public:
numberList()
{
numberStore = new int[8];
} // default constructor
void setter() // not sure what goes here
{
//not sure what goes here
}
private:
int* numberStore;
};
int main()
{
numberList list1;
list1.setter(1,2,3,4,5,6,7)
}
I'd like for the list1.setter() to take all the values to be put into the array. I think memcpy() could be used here, but am unsure.
I understand there's a concept of operator overloading, but am unsure how utilise this. Any help would be appreciated :-)
EDIT: the assignment requires me not to use standard libraries unfortunately :-(
From your comment that you can't use the STL the solution is to use raw C-pointer arithemtics.
To your setter function you can give a int* pointer and the number of elements:
void setter(int* arr, int n) {
for(int i = 0; i < n; i++)
numberStore[i] = arr[i]
}
Then you can call your setter function in main() like this:
int main() {
numberList list1;
int arr[] = {1,2,3,4,5,6,7};
list1.setter(arr, 7);
}
#include "pch.h"
#include <iostream>
#include <string>
int* my_array = new int[10];
void setter(int* arr, int n)
{
memcpy(&my_array[0], &arr[0], n * sizeof(int));
}
int main()
{
int* arry = new int[10];
for (int i=0; i< 10; i++)
{
arry[i] = i;
}
setter(arry, 10);
for (int i = 0; i < 10; i++)
{
std::cout<<(std::to_string(my_array[i]));
}
}

Segmentation fault when creating a row-major array

I'm trying to implement a row-major array, which is basically a single dimension representation of a 2D array.
This is my class definition
class RMA{
public:
RMA(){
size_=0;
row_=0;
column_=0;
arr_ = new double[size_];
}
RMA(int n, int m){
size_ = n*m;
column_ = m;
row_ = n;
if(size_== 0) arr_ = 0;
else arr_ = new double[size_];
}
RMA(const RMA& arr) {
size_ = arr.size_;
if(this != &arr){
delete [] arr_;
arr_ = new double[size_];
for(int i=0; i<size_; i++){
arr_[i] = arr.arr_[i];
}
}
return *this;
}
const double& operator() (int n, int m) const{
return arr_[n*column_+m];
}
double& operator()(int n, int m){
return arr_[n*column_+m];
}
~RMA(){delete[] arr_ ;}
private:
int size_;
int column_;
int row_;
double* arr_;
}
I've a calling function which creates the array.
RMA create_array() {
RMA arr;
arr = RMA(N, M);
std::cout<<"success";
return arr;
}
And this is my client
int main(int argc, char* argv[]) {
RMA arr = create_array();
return 0;
}
I end up getting segmentation fault. What am I doing wrong.
You use operations, that instead of cloning array, take a shallow copy of an object, and when destructors are used, they try to release the same memory block.
Implement the following operations:
RMA::RMA(const RMA&); // copy constructor - clone buffer
RMA& operator=(const &RMA); // assignment - clone buffer, release old
Also instead of:
RMA rma;
rma = RMA(a,b);
Use:
RMA rma = RMA(a,b) or RMA rma(a,b);
Edit: constructor code:
RMA::RMA(const RMA &rma) : size_(0), row_(0), column_(0), buffer_(0)
{
*this = rma;
}
RMA &operator=(const RMA&rma)
{
double *old = buffer_;
size_ = rma.size_;
row_ = rma.row_;
column_ = rma.column_;
buffer_ = new double[size_];
memcpy(buffer_, rma.buffer_, sizeof(buffer_[0]) * size_);
delete []old;
return *this;
}
The best solution is to get rid of all the new/delete, copy-constructors, and fluff. Use a private member variable to manage the memory, and follow the Rule of Zero. Like this:
struct RMA
{
RMA(size_t r = 0, size_t c = 0)
: row(r), column(c), arr(r * c) {}
const double& operator() (int n, int m) const
{ return arr[n * column + m]; }
double& operator() (int n, int m)
{ return arr[n * column + m]; }
private:
std::vector<double> arr;
size_t row, column;
};
That's it. You should not write any copy-constructor, assignment operator, move whatever, because the default-generated ones already do the right thing.
NB. row is actually redundant in my example too, you could remove it and calculate it when needed as arr.size() / column.
You could use .at( ) instead of [ ] on vector in order to throw an exception for out-of-bounds access, instead of causing undefined behaviour.

Operator overloading with pointers

Hi there c++ programmers. I am encountering issue that i cant understand.I have strip the following program for readability reasons and left what i am having trouble with. I am trying to overload a + and = operators, but with dynamically created arrays. By themselves the + and = operator methods are producing the right results. However when i try to assign the result from the + operator to *poly3 i get "*poly3 need to be initialized" from the compiler. If i do initialize it nothing gets assign to it( i mean the result from +).
My question, what is the right way to do this. I need the result poly3 to be dynamic array or a pointer as well so i can use it latter.
Thanks a lot for the help in advance.
class Polynomial
{
private:
int *poly;
int size;
public:
Polynomial();
Polynomial(int);
Polynomial(string,int);
~Polynomial();
void setPoly(string);
int *getPoly() const;
Polynomial operator+(Polynomial&);
void operator=(const Polynomial&);
};
Polynomial::Polynomial(string polyInput, int s)
{
size = s+1;
poly = new int[size];
//set all coef position to 0
for(int i = 0; i < size; i++){
*(poly + i) = 0;
}
setPoly(polyInput);
}
Polynomial Polynomial::operator+(Polynomial &polyRight)
{
Polynomial *result = new Polynomial(size);
for(int i = 0; i < size; i++)
result->poly[i] = poly[i] + polyRight.poly[i];
return *result;
}
void Polynomial::operator=(const Polynomial &polyRight)
{
size = polyRight.size;
for(int i = 0; i < size; i++){
*(poly + i) = polyRight.poly[i];
}
}
int main()
{
int highestExp = 4;
Polynomial *poly1;
Polynomial *poly2;
Polynomial *poly3;// = new Polynomial(highestExp); // for the result
string input1,input2;
ifstream inputFile("data.txt");
getline(inputFile, input1);
getline(inputFile, input2);
poly1 = new Polynomial(input1,highestExp);
poly2 = new Polynomial(input2,highestExp);
*poly3 = *poly1 + *poly2;
system("pause");
return 0;
}
The assignment operator must return a reference to the instance assigned to:
Polynomial& operator=(const Polynomial&);
In the implementation, you should return *this;. You must also make sure that the implementation is robust against assignment of polynomials of different size. That doesn't seem to be the case currently.
Then, in main, poly3 is uninitialized. Just drop the pointers and instantiate all the polys as automatic storage, local variables in main():
Polynomial poly1(input1,highestExp);
Polynomial poly2(input2,highestExp);
Polynimial poly3 = poly1 + poly2;
As an aside, your operator+ has a memory leak. You should not be using new int it. Create a local Polynomial and return it.
Furthermore, you have no destructor taking care of releasing resources, so every instantiation of a Polynomial results in a memory leak. Your class has too many responsibilities: managing resources and being a polynomial. You should let a different class take care of the memory management. Use std::vector<int>, or, if this is an exercise, write your own dynamic array-like class, and use that inside Polynomial.
Pointers point to a memory location explicitly assigned to it.
Ptr* pt = new Ptr();//pt points to a valid address in memory
Ptr* pt;//pt has not yet been initialized
i.e. it is not pointing to any valid address. Using pt can cause unexpected behavior. By default pointers should point to NULL and they should be checked for !NULL and then used.
In above code Polynomial *poly3; is not pointing to any location and when we do
*pol3 we are actually dereferencing a location which had never been created.
When you do Polynomial *poly3 = new Polynomial(highestExp);, you are pointing poly3 to some valid location and hence *pol3 = *poly1 + *pol2 makes sense. But beware when you do a new for poly3 you are creating one more heap storage, so you have to make sure that you free all memory assigned on heap.
A better solution would be have your opertor + return a valid pointer and have it mapped to poly3 like this:
Polynomial* Polynomial::operator+(Polynomial &polyRight)
{
Polynomial *result = new Polynomial(size);
for(int i = 0; i < size; i++)
result->poly[i] = poly[i] + polyRight.poly[i];
return result;
}
//and in main
poly3 = (*poly1 + *poly2);
To start with:
// allow adding const
const Polynomial Polynomial::operator+(const Polynomial &polyRight) const
{
Polynomial result(size); // don't use a pointer
for(int i = 0; i < size; i++)
result.poly[i] = poly[i] + polyRight.poly[i];
return result;
}
make the return const to avoid the silliness of (A+B)=C;
Polynomial& Polynomial::operator=(const Polynomial &polyRight) // return a reference
{
size = polyRight.size;
for(int i = 0; i < size; i++){
*(poly + i) = polyRight.poly[i];
}
return *this; // allow chaining
}
Note that once you patch this up that you need to guard against A=A in operator =() :
if (this == &rhs)
{
// do stuff
}
return *this;
Here is quite a cleanup, with some slightly different functionality and better memory management. Since I didn't know the format of your input file, I've skipped the reading from file part, and just use a new constructor of the form Polynomial(countOfExponents, x0, x1, x2, x3, ... , xn)
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <fstream>
#include <iostream>
#include <sstream>
#include <cstdarg>
using namespace std;
class Polynomial
{
private:
int *poly;
int size;
public:
Polynomial() : size(0), poly(NULL) {}
Polynomial(int size, ... );
~Polynomial() { delete[](poly); }
void allocate(int size) {
if (NULL!=poly) {
delete[](poly);
}
Polynomial::size = size;
poly=new int[size];
}
Polynomial operator+(const Polynomial&) const;
Polynomial &operator=(const Polynomial&);
int exponent(int p) const {
return (p<size) ? poly[p] : 0;
}
string str() const;
};
Polynomial::Polynomial(int size, ...) : size(size) {
va_list varargs;
va_start(varargs, size);
poly = new int[size];
for (int i=0; i<size; i++) {
poly[i] = va_arg(varargs, int);
}
va_end(varargs);
}
Polynomial Polynomial::operator+(const Polynomial &polyRight) const
{
int newSize = max(size, polyRight.size);
Polynomial result;
result.allocate(newSize);
for(int i = 0; i < newSize; i++)
result.poly[i] = exponent(i) + polyRight.exponent(i);
return result;
}
Polynomial &Polynomial::operator=(const Polynomial &polyRight)
{
allocate(polyRight.size);
memcpy(poly, polyRight.poly, sizeof(int) * size);
return *this;
}
string Polynomial::str() const {
stringstream out;
for (int i=size-1; i>=0; i--) {
out << poly[i];
if (0<i) {
out << " ";
}
}
return out.str();
}
int main()
{
Polynomial one(3, 1, 2, 3);
Polynomial two(3, 2, 3, 4);
cout << one.str() << endl;
cout << two.str() << endl;
Polynomial three = one + two;
cout << three.str() << endl;
return 0;
}
Note that I'm also being careful not to address memory that might not exist when working with polynomials of different sizes. Accessing poly[n] on a polynomial with fewer than n exponents would cause trouble. Instead, use the exponent(n) function, which will return 0 for all exponents higher than those inside the polynomial.

dynamic array member variables

Hey i'm new to c++ and still working out its perticularities. I'm having the darnedest time trying to figure out whats going wrong with this code. I've stepped through it and everything is calculating correctly. The issue is that value_array in the base class doesn't seem to be retaining the values once the derived class Calculate function ends. I think i've declared and allocated the array properly. I'm stumped...
#include <iostream>
class Indicator
{
protected:
double * value_array;
double * input_array;
int input_size;
public:
Indicator(double input[], int size)
{
input_array = input;
input_size = size;
value_array = new double[size]; // issue with value_array
}
double operator[] (int index) { return value_array[index]; }
void virtual Calculate() {}
~Indicator() { delete[] value_array; }
};
class SMA : public Indicator
{
private:
int nperiod;
double sum;
public:
SMA(double input[], int size, int period) : Indicator(input, size)
{
nperiod = period;
sum = 0;
Calculate();
}
void Calculate();
};
void SMA::Calculate()
{
for (int i=0; i<input_size; i++)
{
if (i > nperiod - 1)
{
sum += input_array[i] - input_array[i-nperiod];
value_array[i] = sum / nperiod;
}
else
{
sum += input_array[i];
value_array[i] = sum / (i+1);
}
}
}
int main(int argc, const char *argv[]) {
double input[] = {1,2,3,4,5,6,7,8,9,10};
Indicator indicator = SMA(input,10,5);
double value = indicator[0];
std::cout << "value: " << value << std::endl;
std::cin.get();
exit(0);
}
Update:
Here is the code implemented with vectors. I wanted to leave the input as double[] to be consistent with other libraries, any other potential issues I should be aware of?
#include <iostream>
#include <vector>
class Indicator
{
protected:
std::vector<double> value_vector;
double * input_array;
int input_size;
public:
Indicator(double input[], int size)
{
input_array = input;
input_size = size;
value_vector.reserve(size);
}
double operator[] (int index) { return value_vector[index]; }
void virtual Calculate() {}
};
class SMA : public Indicator
{
private:
int nperiod;
double sum;
public:
SMA(double input[], int size, int period) : Indicator(input, size)
{
nperiod = period;
sum = 0;
Calculate();
}
void Calculate();
};
void SMA::Calculate()
{
for (int i=0; i<input_size; i++)
{
if (i > nperiod - 1)
{
sum += input_array[i] - input_array[i-nperiod];
value_vector.push_back(sum / nperiod);
}
else
{
sum += input_array[i];
value_vector.push_back(sum / (i+1));
}
std::cout << "sma: " << value_vector[i] << std::endl;
}
}
int main(int argc, const char *argv[]) {
double input[] = {1,2,3,4,5,6,7,8,9,10};
Indicator indicator = SMA(input,10,5);
for (int i=0; i<10; i++)
{
std::cout << "main: " << indicator[i] << std::endl;
}
std::cin.get();
exit(0);
}
That's because you're violating the Rule of Three. Since your class manages a resource, it needs a copy constructor and an assignment operator. I strongly suggest replacing any T* data member with a std::vector<T> data member. Then you don't need to write those special member functions manually.
Hia,
a few things are wrong.
As FredOverflow says you need a copy constructor and assignment, something like:
Indicator::Indicator(const Indicator& other)
{
input_size = other.input_size;
//direct copy of reference as indicator doesn't own this data
//Note a shared pointer (such as boost::shared_ptr) would be better than a naked reference
input_array = other.input_array;
//construct a new set of data
value_array = new double[input_size];
//do you want to copy the data too? maybe a memcpy follows?
memcpy(value_array, other.value_array, input_size*sizeof(double));
}
Then you need an assignment
Indicator&
Indicator::operator=(const Indicator& other)
{
//make sure you are not assigning itself
if(this != &other)
{
input_size = other.input_size;
//direct copy of reference as indicator doesn't own this data
//Note a shared pointer (such as boost::shared_ptr) would be better than a naked reference
input_array = other.input_array;
//destroy old data and construct a new set of data
delete[] value_array;
value_array = new double[input_size];
//do you want to copy the data too? maybe a memcpy follows?
memcpy(value_array, other.value_array, input_size*sizeof(double));
}
return *this;
}
You probably also want to make the destructor virtual - see here for why -
it helps prevent memory leaks in the destructor of SMA
virtual ~Indicator() { delete[] value_array; }
Use std::vector instead of raw arrays.
std::vector handles all the memory management and copying and so forth.
Cheers & hth.,