-1

I'm designing a template class that has an internal array to store data. It overloads the + operator to add each array element to the corresponding array element in the other operand. It overloads the = operator to perform a deep copy and assign it to another instance of the class.

It compiles fine and the overloading works fine when I use it for a float, which was what the class was originally made for. However when I turn and use it for an int, it gets to the overloaded + operator and I get a segmentation fault. Can someone please point me in the right direction?

Array.h

#include <iostream>

using namespace std;

template<class T>
class Array {


private:
    int maxSize;
    T* internal;

    void init();

public:
    ~Array();

    Array(int);

    template<class Y>
    friend Array<Y> operator+ (const Array<Y> &x, const Array<Y> &y);

    template<class Y>
    friend ostream &operator<<(ostream &out, const Array<Y> &y);

    Array( const Array<T> &y );

    void operator=( const Array<T> &t);

    int getSize() const;    

    void setValue(int index, T number);

    T getValue(int index) const;

};

template<class T>
Array<T>::Array(int pass) {

    this->maxSize = pass;
    internal = new T[maxSize];
    init();

}

template<class T>
Array<T>::~Array(){

    if( internal ) delete[] internal;

}

template<class T>
void Array<T>::init(){
    for(int i =0; i < (maxSize); i++){
        internal[i] = 0;
    }

}

template<class T>
void Array<T>::operator=(const Array<T>& t){

    // if not copying self

    if( this != &t ){
        maxSize  = t.maxSize;

        // delete any existing memory
        if( internal ) delete[] internal;
        // allocate memory for object
        init();
        // copy dynamic memory

        for(int i = 0; i < maxSize; ++i )
              this->setValue(i, t.getValue(i)) ;

     }

}
template<class T>
Array<T>::Array( const Array<T> &t ){

// This calls overloaded assignment operator

     *this = t;

}

template<class Y>
Array<Y> operator+ (const Array<Y> &x, const Array<Y> &y){
        Array<Y> returnable(x.getSize());
        for(int i = 0; i < y.getSize(); i++){
            returnable.setValue(i, (x.getValue(i) + y.getValue(i)));
        }
        return returnable;
    }

template<class Y>
ostream &operator<<(ostream &out, const Array<Y> &y){
        out << "Array Result" << endl;
        out << "------------" << endl;
        for(int i = 0; i < y.getSize(); i++ ){
            out << i << "    " << y.getValue(i) << endl;
        }

        return out;
    }

template<class T>
int Array<T>::getSize() const{
        return this->maxSize;
    }

template <class T>
void Array<T>::setValue(int index, T number){
        *&internal[index] = number; 
    }

template<class T>
T Array<T>::getValue(int index) const{
        return *&internal[index];
    }

main.cpp

#include <iostream>
#include "Array.h"

using namespace std;

int main() {

Array<float> a(3);
a.setValue(0, 1.1);
a.setValue(1, 2.2);
a.setValue(2,3.3);

Array<float> b(3);
b.setValue(0, 1.1);
b.setValue(1, 2.2);
b.setValue(2,3.3);

cout << "a: " << endl << a << endl;
cout << "b:" << endl << b << endl;

Array<float> c(3);

c = a + b;

cout << "c: " << endl << c << endl;
cout << "a: " << endl << a << endl;
cout << "b:" << endl << b << endl;


Array<int> d(3);
d.setValue(0, 1);
d.setValue(1, 2);
d.setValue(2,3);

Array<int> e(3);
e.setValue(0, 1);
e.setValue(1, 2);
e.setValue(2,3);

Array<int> f(3);

f = d + e; // fails here on the addition operator I believe

cout << f;
return 0;
}

1 Answers1

1

Your assignment operator is broken. First you call delete on internal, invalidating it. Then you call init, which assigns to the elements of internal. You need a reallocation between those two steps.

On another note, your copy constructor is broken too. Set internal to nullptr first. Otherwise, the assignment operator will be calling delete on an unititialized pointer.

On another note, your addition operator is broken. It assumes that the two arrays are the same size. If the second operand is bigger, you will have undefined behavior accessing elements that do not exist.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • I set internal to nullptr, and fixed the memory issue on your suggestion. It works. In the assignment we are told to assume both instances of Array are the same length. I'm not taking the class for school credit thats why I asked here. – Craig Swearingen Jul 19 '14 at 16:31