0

Alright, I'm trying to implement a simple 2D matrix class right now. This is what it looks like so far:

template <typename Type>
class dyMatrix {
    private:
        Type *mat;

        int width, height;
        int length;

    public:
        dyMatrix (int _width, int _height)
            : width(_width), height(_height), mat(0)
        {
            length = width * height;
            mat = new Type[length];
        };

        // ---

        int getWidth() {
            return width;
        };

        int getHeight() {
            return height;
        };

        int getLength() {
            return length;
        }

        // ---

        Type& operator() (int i, int j) {
            return mat[j * width + i];
        };

        Type& operator() (int i) {
            return mat[i];
        };

        // ---

        ~dyMatrix() {
            delete[] mat;
        };
};

To test it, and compare with static multi-dimensional arrays, I wrote the following snippet of code:

#include <iostream>
using namespace std;

/* matrix class goes here */

struct Coord {
    int x, y;

    Coord()
        : x(0), y(0)
    {};

    Coord (int _x, int _y)
        : x(_x), y(_y)
    {};

    void print() {
        cout << x << ", " << y;
    };
};

int main() {
    dyMatrix<Coord> adabo(5, 7);
    Coord inakos[5][7];

    int i = 5, j = 0;

    adabo(i, j) = *(new Coord(i, j));
    inakos[i][j] = *(new Coord(i, j));

    inakos[i][j].print();
    adabo(i, j).print();

    return 0;
}

"Adabo" and "Inakos" being arbitrarily chosen names. Upon execution, inakos prints its contents but the program crashes before adabo can do anything. Another interesting thing is that, if I give i and j values other than 5 and 0, like 5 and 1, respectively, it works fine.

I don't know what exact numbers work and which make the program go haywire, I only know that there's a irregularity here. What am I possibly doing wrong? I'm an amateur at C++, so I may or not have misused something in any of the structures.

If anyone also has the time, I'd very much like to know if there's any other error of notice in my matrix class. Anything that's not possibly related to the problem, but is a fallacy nevertheless.

I've also tested it with the following main(), but it still crashes after inakos prints its contents in [5][1]. Maybe it has to do not with dyMatrix, but a loosely-implemented Coord?

int main() {
    dyMatrix<Coord> adabo(5, 7);
    Coord inakos[5][7];

    for (int i = 0; i < adabo.getHeight(); i++) {
        for (int j = 0; j < adabo.getWidth(); j++) {
            adabo(i, j) = *(new Coord(i, j));
            inakos[i][j] = *(new Coord(i, j));

            inakos[i][j].print();
            cout << "; ";
        }
        cout << "\n\n";
    }
    cout << "\n\n\n";

    Coord temp;
    for (int i = 0; i < 7; i++) {
        for (int j = 0; j < 5; j++) {
            temp = adabo(i, j);

            temp.print();
            cout << "; ";
        }
        cout << "\n\n";
    }

    return 0;
}

edit: It hasn't to do with Coord. Just tested with a dyMatrix of ints and static matrix of ints, and it crashes after [5][0] nevertheless.

2
  • I would encourage you to invest time into learning to use a debugger. This will pay for itself many times over. Commented Mar 2, 2013 at 15:18
  • @NPE Yeah, I could try. Code::Blocks' debugger ain't very intuitive, however, but hey it's there. Commented Mar 2, 2013 at 15:21

1 Answer 1

1

In your first example, you declared inakos[5][7] so the indices range from 0 to 4 and 0 to 6. inakos[5][0] and inakos[5][1] could therefore crash.

In your second example, you again declare inakos[5][7] yet you let the first index loop from 0 to 6. Again inakos[i][j] can crash. One fix is to switch your indices (ie, change to i<adabo.getWidth() and j<adabo.getHeight()).

Sign up to request clarification or add additional context in comments.

1 Comment

Hmm, indeed. I was going for columns first, lines second.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.