-2

Consider the following code:

    int* solve(int input[], int len) {

    //Processing and building the calc array. Can be ignored. 

    int calc[3*(len-1)];
    calc[0] = input[0];
    calc[1] = 1;
    calc[2] = 1;
    for (int b = 1; b < len - 1; b++) {
        calc[3*b] = 0;
        calc[3*b + 1] = 0;
        calc[3*b + 2] = 0;
    }
    if (input[0] < input[1]) {
        calc[3] = input[1];
        calc[4] = 0;
        calc[5] = 1;
    } else {
        calc[3] = input[0];
        calc[4] = 1;
        calc[5] = 0;
    }
    for (int i = 2; i < len - 1; i++) {
        for (int j = 0; j < i; j++) {
            if ((i - j > 1 || calc[3*j + 2] == 0) && calc[3*j] + input[i] > calc[3*i]) {
                calc[3*i] = calc[3*j] + input[i];
                calc[3*i + 1] = calc[3*j + 1];
                calc[3*i + 2] = 1;
            } else if (input[i] > input[j] && calc[3*i] < calc[3*j] - input[j] + input[i]) {
                calc[3*i] = calc[3*j] - input[j] + input[i];
                calc[3*i + 1] = calc[3*j + 1];
                calc[3*i + 2] = 1; 
            } else if (calc[3*i] < calc[3*j]) {
                calc[3*i] = calc[3*j];
                calc[3*i + 1] = calc[3*j + 1];
                calc[3*i + 2] = 0; 
            }
        }


    }

    //Printing the array

    cout<<"Calc array: ";
    for (int a = 0; a < len - 1; a++) {
        cout<<"("<<calc[3*a]<<" "<<calc[3*a + 1]<<" "<<calc[3*a+2]<<") ";
    }
    cout<<endl;

    //Returning a pointer to the array

    int *pointer = calc;
    return pointer;
}

int main() {

    //Taking input. Can be ignored. 

    int len;
    cin>>len;
    int input[len];
    for (int i = 0; i < len; i++) {
        cin>>input[i];
    }

    //Assigning another pointer to the array that the solve() function returns.

    int *a = solve(input, len);
    int *b;

    //Printing the array that the pointer points to.  

    cout<<"A Array: ";
    for (int x = 0; x < len - 1; x++) {
        cout<<"("<<a[3*x]<<" "<<a[3*x + 1]<<" "<<a[3*x+2]<<") ";
    }
    cout<<endl;

    //Ignore code from here. 

    int c;
    if (a[3*(len - 2) + 1] == 1) {
        input[0] = -10*10*10*10;
        b = solve(input, len);

        if (b[3*(len - 2) + 2] == 1) {
            if  (input[len-1] > input[len-2]) {
                c = b[3*(len - 2)] - input[len-2] + input[len - 1];
                cout<<c<<endl;
            } else {
                c = b[3*(len - 2)];
            }
        } else {
            c = b[3*(len - 2)] + input[len-1];
        }
        if (c < a[3*(len - 2)]) {

            cout<<a[3*(len - 2)];
        } else {
            cout<<c<<endl;
            cout<<a[3*(len - 2)]<<" "<<a[3*(len - 2) + 1]<<" "<<a[3*(len - 2) + 2];
            cout<<"This route"<<endl;
        }
    } else {
        input[1] = -10*10*10*10;
        b = solve(input, len);
        if (a[3*(len - 2) + 2] == 1) {
            if  (input[len-1] > input[len-2]) {
                c = a[3*(len - 2)] - input[len-2] + input[len - 1];
            } else {
                c = a[3*(len - 2)];
            }
        } else {
            c = a[3*(len - 2)] + input[len-1];
        }
        if (c > b[3*(len - 2)]) {
            cout<<b[3*(len - 2)];
        } else {
            cout<<c;
        }
    }

}

Now the problem here is, that when I print the calc array inside the solve() function the first time it prints perfectly and gives the following desired output:

Calc array: (10 1 1) (10 1 0) (12 1 1) (15 1 1) (19 1 1)

But when I print it again inside the main() function, I get the following output:

A Array: (135712 0 1259266624) (2045 1 0) (4792936 0 32) (15 4792936 0) (2357952 0 4792936)

I have just migrated from Python to C++, and I find it extremely cumbersome, at times such as these. I have tried all sorts of modifications to the code but I still can't figure out the problem. Any help would be appreciated.

Gerard
  • 679
  • 2
  • 9
  • 16

1 Answers1

2

calc is a local variable whose lifetime starts at it's definition and ends when the function exits.

Since you're returning a pointer to it when exiting the function, the dereferencing of said pointer will be undefined behaviour (since the "object" behind it no longer exists).

If you want a variable to survive function return, you'll need to do something like allocating it dynamically, changing:

int calc[3*(len-1)];

to:

int *calc = new int [(3 * (len - 1)];

and then ensuring you delete[] it in the caller when you're done with it.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953