Original Post
Hi I've have been playing around with an array template class and I've run into something that I wuold like to get clerance on. First of all the problem arises when the destructor is checking for existance of the member array I'm using: This works: ~Array() { if(m_aArray == NULL) { delete[] m_aArray; } m_aArray = NULL; } This doesn't: ~Array() { if(m_aArray != NULL) { delete[] m_aArray; } m_aArray = NULL; } Now isn't it more logical to check if the array does exist and then delete it other than delete it only if it is doesn't exist? I would think so I'm pretty convinced that the snippet that doesn't work is semantically the right way but is it done correctly syntactically? I'm using Visual C++ 2005 Express and the debugger breaks at the line that says delete[] m_aArray. Can't figure this out. Hope that somebody can;-) regards thallish PS. Just throwing the entire class here. Maybe something else is wrong: And this is my main [Edited by - thallish on January 25, 2006 12:45:28 PM]
template <class T>
class Array
{
public:
// Constructor: Construct a new array of a type T with specified size
Array (int size)
{
m_aArray = new T[size];
m_iSize = size;
}
// Destructor: If array exists delete it
// otherwise just set pointer to NULL
~Array()
{
if(m_aArray == NULL)
{
delete[] m_aArray;
}
m_aArray = NULL;
}
// Returns the current size of the array
int GetSize()
{
return m_iSize;
}
// If index is not inside bounds, return false
// If index is inside bounds, add item to array at index and return true
bool Add(T item, int index)
{
if(index < 0)
{
// Illegal index!
return false;
}
// If larger then current size
// resize array to size specified in index
if(index > m_iSize - 1)
{
if(!this->ReSize(index))
return false
}
m_aArray[index] = item;
return true;
}
// Resize array to double the current size
bool DoubleSize()
{
// Declaring variables
int new_size;
T* new_array;
// Setting new size of array
new_size = this->GetSize();
// If size of array is less or equal to 0
// size is set to 1
if(new_size <= 0)
{
new_size = 1;
}
// Doubling size
new_size = new_size * 2;
// Setting size of tempoary array to the new size
new_array = new T[new_size];
if(new_array == NULL)
return false;
// Transfering all elements from old array to new array
for(int i = 0; i < m_iSize; i++)
{
new_array = m_aArray;
}
// Setting the new values
m_aArray = new_array;
m_iSize = new_size;
// If the tempoary array exists delete it
if(new_array != NULL)
delete[] new_array;
new_array = NULL;
return true;
}
// Resizes array to specified size
bool ReSize(int new_size)
{
// If illegal argument is given return false
if(new_size < 0)
{
return false;
}
// Setting size of temporary array to specified size
T* new_array = new T[new_size];
if(new_array == NULL)
return false;
int min;
// Determine which array is smallest
if(m_iSize < new_size)
{
min = m_iSize;
}
else
{
min = new_size;
}
// Transfering items from old to temporary array
for(int i = 0; i < min; i++)
{
new_array = m_aArray;
}
// Setting new values
m_aArray = new_array;
m_iSize = new_size;
// If temporary array exist delete it
if(new_array != NULL)
delete[] new_array;
new_array = NULL;
return true;
}
T& operator[] (int index)
{
return m_aArray[index];
}
private:
T* m_aArray;
int m_iSize;
};
#include <iostream>
using namespace std;
#include "array_template.h"
int main()
{
int size = 5;
Array<float> here(size);
cout << here.GetSize() << endl;
here.ReSize(19);
cout << here.GetSize() << endl;
here.DoubleSize();
cout << here.GetSize() << endl;
int a;
cin >> a;
if(a)
return 0;
}