Hi everyone:
I am having problems with a class I created for making 2D dynamic arrays.
I'm using Xcode and my program keeps crashing with the message, "out of memory." I'm pretty sure that my constructor and destructor are OK. Someone suggested that I need a copy/assignment constructor. I have tried to write these, but the crashes are still happening.
I don't feel confident that I understand copy/assignment constructors as well as I should, so it could be that I've made a silly mistake somewhere. If anyone could take a look at my code and comment on any possible problems, I would really appreciate it!
.h file:
#include<string>
#include<iostream>
#include<vector>
using namespace std;
template <typename T>
class Array2d
{
int rows, cols;
T** array;
public:
Array2d ( int rowsPub, int colsPub );
~Array2d ( );
Array2d ( const Array2d<T>& m ); //copy constructor
Array2d<T>& operator=( const Array2d<T>& m ); //copy assignment
void resizeArray2d( int rowsPub, int colsPub );
void deleteArray2d( );
// Other functions for setting values in the array.
T& operator ( ) ( int x, int y );
const T& operator( ) ( int x, int y ) const;
}
.cpp file
#include "Array2d.h"
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <new>
template <typename T>
Array2d<T>::Array2d(int rowsPub, int colsPub)
: rows(rowsPub), cols(colsPub)
{
array = new T* [ rows ];
for (int i = 0; i < rows; i++)
array = new T[ cols ];
// Initialization
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
array[j] = 0;
}
template <typename T>
Array2d<T>::~Array2d()
{
for(int i = 0; i < rows; i++)
{
delete[ ] array;
}
delete[ ] array;
}
// COPY/ASSIGNMENT OPERATORS BELOW
template <typename T>
Array2d<T>::Array2d(const Array2d<T>& m)
: rows(m.rows), cols(m.cols)
{
array = new T* [ rows ];
for (int i = 0; i < rows; i++)
{
array = new T[ cols ];
}
for (int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
array[j] = m.array[j];
}
}
}
template <typename T>
Array2d<T> &Array2d<T>:perator=(const Array2d<T>& m)
{
if (this == &m)
{
return *this;
}
else
{
if (rows != m.rows || cols != m.cols)
{
this->~Array2d(); rows = m.rows; cols = m.cols;
array = new T* [ rows ];
for (int i = 0; i < rows; i++)
{
array = new T[ cols ];
}
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
array[j] = m.array[j];
}
}
return *this;
}
// Functions
template <typename T>
void Matrix2d<T>::resizeArray2d(int rowsPub, int colsPub)
{
for(int i = 0; i < rows; i++)
delete[ ] array;
delete[ ] array;
array = new T * [ rows ];
for(int i = 0; i < rows; i++)
array = new T[ cols ];
}
template <typename T>
T& Array2d<T>:perator() (int x, int y)
{
return array[x][y];
}
template <typename T>
const T& Array2d<T>:perator() (int x, int y) const
{
return array[x][y];
}
template class Array2d<int>;
template class Array2d<double>;
template class Array2d<long>;
I am having problems with a class I created for making 2D dynamic arrays.
I'm using Xcode and my program keeps crashing with the message, "out of memory." I'm pretty sure that my constructor and destructor are OK. Someone suggested that I need a copy/assignment constructor. I have tried to write these, but the crashes are still happening.
I don't feel confident that I understand copy/assignment constructors as well as I should, so it could be that I've made a silly mistake somewhere. If anyone could take a look at my code and comment on any possible problems, I would really appreciate it!
.h file:
#include<string>
#include<iostream>
#include<vector>
using namespace std;
template <typename T>
class Array2d
{
int rows, cols;
T** array;
public:
Array2d ( int rowsPub, int colsPub );
~Array2d ( );
Array2d ( const Array2d<T>& m ); //copy constructor
Array2d<T>& operator=( const Array2d<T>& m ); //copy assignment
void resizeArray2d( int rowsPub, int colsPub );
void deleteArray2d( );
// Other functions for setting values in the array.
T& operator ( ) ( int x, int y );
const T& operator( ) ( int x, int y ) const;
}
.cpp file
#include "Array2d.h"
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <cmath>
#include <new>
template <typename T>
Array2d<T>::Array2d(int rowsPub, int colsPub)
: rows(rowsPub), cols(colsPub)
{
array = new T* [ rows ];
for (int i = 0; i < rows; i++)
array = new T[ cols ];
// Initialization
for(int i = 0; i < rows; i++)
for(int j = 0; j < cols; j++)
array[j] = 0;
}
template <typename T>
Array2d<T>::~Array2d()
{
for(int i = 0; i < rows; i++)
{
delete[ ] array;
}
delete[ ] array;
}
// COPY/ASSIGNMENT OPERATORS BELOW
template <typename T>
Array2d<T>::Array2d(const Array2d<T>& m)
: rows(m.rows), cols(m.cols)
{
array = new T* [ rows ];
for (int i = 0; i < rows; i++)
{
array = new T[ cols ];
}
for (int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
array[j] = m.array[j];
}
}
}
template <typename T>
Array2d<T> &Array2d<T>:perator=(const Array2d<T>& m)
{
if (this == &m)
{
return *this;
}
else
{
if (rows != m.rows || cols != m.cols)
{
this->~Array2d(); rows = m.rows; cols = m.cols;
array = new T* [ rows ];
for (int i = 0; i < rows; i++)
{
array = new T[ cols ];
}
}
}
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
array[j] = m.array[j];
}
}
return *this;
}
// Functions
template <typename T>
void Matrix2d<T>::resizeArray2d(int rowsPub, int colsPub)
{
for(int i = 0; i < rows; i++)
delete[ ] array;
delete[ ] array;
array = new T * [ rows ];
for(int i = 0; i < rows; i++)
array = new T[ cols ];
}
template <typename T>
T& Array2d<T>:perator() (int x, int y)
{
return array[x][y];
}
template <typename T>
const T& Array2d<T>:perator() (int x, int y) const
{
return array[x][y];
}
template class Array2d<int>;
template class Array2d<double>;
template class Array2d<long>;