0

My program consists of two classes. The first creates a 2D array and populates it with user input. The first class works correctly, and when I call it in main it is able to create and print a 2D array. However, I am attempting to pass a pointer to that 2D array to the second function to calculate the determinant of the matrix. However, my program keeps crashing after the determinant function is called. Why am I unable to multiply, add, or subtract these array elements?

Here is the implementation file for the determinant class:

#include <iostream>
#include "det.hpp"

using std::cout;
using std::endl;

Det::Det() {

};
int Det::determinant(int **pointerToArray, int arraySize) {
    int determinant;
    cout << "Calculating the determinant..." << endl;
    if (arraySize == 2) {
        determinant = (pointerToArray[0][0] * pointerToArray[1][1]) - 
                      (pointerToArray[1][0] * pointerToArray[1][0]);
    } else if (arraySize == 3) {
        determinant = (pointerToArray[0][0] * ((pointerToArray[1][1] * pointerToArray[2][2]) -
                      (pointerToArray[1][2] * pointerToArray[2][1]))) -
                      (pointerToArray[0][1] * ((pointerToArray[1][0] * pointerToArray[1][1]) -
                      (pointerToArray[1][2] * pointerToArray[2][0]))) +
                      (pointerToArray[0][2] * ((pointerToArray[1][0] * (pointerToArray[2][1]) -
                      (pointerToArray[1][1] * pointerToArray[2][0])));
    } else {
        return determinant;
    };
};

Here is how is the part of main which I am calling the function:

//this is the original object
Matrix* point = new Matrix();
    //this is where I retrieve the data from the first function
    int * tempPoint = point->readMatrix(newArray, squareSize);
    /*this is where I call the determinant with
    a pointer to the original array as a parameter*/
    calculate.determinant(&tempPoint, squareSize);
csdavido
  • 1
  • 3
  • Why bother with a class that has no state (member variables)? Lose the class and turn `determinant` into a [free function](http://stackoverflow.com/questions/4861914/what-is-the-meaning-of-the-term-free-function-in-c). Less muss, less fuss, and less overhead all-around. On re-think, why not move determinant into the Matrix class? Seems like a logical operation to perform on a matrix. – user4581301 Sep 28 '16 at 04:34
  • That was my first thought actually. This is a school assignment and the requirements specify two header files, two implementation files, as well as a main. – csdavido Sep 28 '16 at 05:40
  • Interesting. You might want to bring that up with the instructor. They could have something sneaky in mind, but they could also be teaching you to write code like a chump. Be good to know which so you can adjust your learning expectations and coding style accordingly. – user4581301 Sep 28 '16 at 05:42

1 Answers1

0

After calling

int * tempPoint = point->readMatrix(newArray, squareSize);

tempPoint points to a 1D array of ints

calculate.determinant(&tempPoint, squareSize);

then takes the address of this pointer to a 1D array and uses it inside as though it were a pointer to a 2D array.

Look at it like this: Say you have an array at address 1000. tempPoint contains this address. You could say that tempPoint == 1000.

tempPoint sits in automatic storage, most likely the stack, and has an address of it's own so the program can find it, say 1000000. This means &tempPoint is 1000000, so inside Det::determinant pointerToArray is address 1000000, and that is nowhere near the data array sitting at address 1000. Det::determinant goes rampaging through the wrong memory and no one knows what will happen. In your case the program goes boom and dies, but it could also limp on broken spitting out the wrong results for weeks. Crom help us if this is in an air traffic control system.

How to fix this:

Matrix::readMatrix needs to return a 2D array that Det::determinant can use.

But... The way this program is structured is not very well thought out. Matrix should contain and control its data, not pass it around blindly to anyone who asks. If someone wants to calculate a determinant they should call a Matrix::determinant method that returns the determinant.

Crack open your text and reread the section on Encapsulation. If you don't have a text or the text is incomprehensible, get and read one of the beginner books from The Definitive C++ Book Guide and List. Trust me, it will save you a ton of time in the long run.

Community
  • 1
  • 1
user4581301
  • 33,082
  • 7
  • 33
  • 54