1

The printResults() method below (called from the main method at the bottom) returns -858993460 for all four values. Why is this? I have confirmed with cout statements that the numbers and calculations are correct within the doCalc() method, so I'm assuming the error is in the way I'm using pointers and calling the printResults() method...

typedef int matrix[2][2] ;

struct matrices {
matrix a;
matrix b;
};

...getInput() method constructs 

matrix* doCalc (matrices m){
matrix toReturn;
char input;
cout << "Which calculation would you like to perform - (M)ultiply, (A)dd, (S)ubtract?";
cin >> input;
switch(input){
case 'M':
    toReturn[0][0] = ((m.a[0][0])*(m.b[0][0]));
    cout << "XX " << ((m.a[0][0])*(m.b[0][0]));
    toReturn[0][1] = (m.a[0][1]*m.b[0][1]);
    cout << "YY " <<  (m.a[0][1]*m.b[0][1]);
    toReturn[1][0] = (m.a[1][0]*m.b[1][0]);
    toReturn[1][1] = (m.a[1][1]*m.b[1][1]);
    break;
case 'A':
    toReturn[0][0] = (m.a[0][0]+m.b[0][0]);
    toReturn[0][1] = (m.a[0][1]+m.b[0][1]);
    toReturn[1][0] = (m.a[1][0]+m.b[1][0]);
    toReturn[1][1] = (m.a[1][1]+m.b[1][1]);
    break;
case 'S':
    toReturn[0][0] = (m.a[0][0]-m.b[0][0]);
    toReturn[0][1] = (m.a[0][1]-m.b[0][1]);
    toReturn[1][0] = (m.a[1][0]-m.b[1][0]);
    toReturn[1][1] = (m.a[1][1]-m.b[1][1]);
    break;
}
return &toReturn;

}

void printResult(matrix m){
cout<<"---RESULT---\n";
cout << m[0][0] << "  " << m[0][1] << "\n";
cout << m[1][0] << "  " << m[1][1] << "\n";

}

void main() {
matrices m = getInput();
cout << m.a[0][0] << "  " << m.a[0][1] << "\n";
cout << m.a[1][0] << "  " << m.a[1][1] << "\n\n";
cout << m.b[0][0] << "  " << m.b[0][1] << "\n";
cout << m.b[1][0] << "  " << m.b[1][1] << "\n";

matrix* calc = doCalc(m);
matrix c = &calc;

printResult(*calc);

}
Cairnarvon
  • 25,981
  • 9
  • 51
  • 65

5 Answers5

2

the matrix toReturn will be destroyed when the function exits, you will need to use some form of persistent memory allocation (look up new and delete). As such all the values are just nonsense.

smitec
  • 3,049
  • 1
  • 16
  • 12
0

matrix toReturn; is a local variable, allocated on the stack. After doCalc returns, its contents are undefined. In this case, the memory at its old address gets overwritten with random garbage.

1''
  • 26,823
  • 32
  • 143
  • 200
0

toReturn will be destroyed with the stack frame when doCalc is finished.

Instead of returning the pointer pass it to the function you are calling.

void doCalc (matrices m, matrix* pReturnMatrix)

matrix* calc = new matrix();
doCalc(m, calc);
james82345
  • 530
  • 4
  • 13
0

toReturn is a local variable in the doCalc function. It will no longer exist when the function returns.

An easy way to fix this is to pass a reference to the matrix that will store the result:

matrix* doCalc (matrices m, matrix& toReturn ){

Then remove this line from doCalc:

matrix toReturn;

And change how the function is called from this:

matrix* calc = doCalc(m);
matrix* calc = doCalc(m);
matrix c = &calc;

printResult(*calc);

To this:

matrix calc;
doCalc(m,calc);
printResult(calc);

There are other ways to fix this, e.g. new/delete as suggested in other places. However, now we will go into who allocates and who owns the memory. It won't be clear from the function signature alone who is responsible for what.

Christian Garbin
  • 2,512
  • 1
  • 23
  • 31
0

You can avoid managing dynamically allocated memory and requiring the caller supply an lvalue if you return by value.

matrix doCalc (const matrices& m)
{
    matrix toReturn;

    // Code here

    return toReturn;
}

You can assign the results to a matrix object or use it in an expression like below.

if(doCalc(m).a[0][0] == 1)
{
     // do something
}

or

matrix mat(doCalc(m));
if(mat.a[0][0] == 1)
{
     // do something
}
Captain Obvlious
  • 19,754
  • 5
  • 44
  • 74