访问时为什么这个指针搞砸了?

时间:2014-01-06 06:38:06

标签: c++

请考虑以下代码:

    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;
        }
    }

}

现在的问题是,当我在calc函数内打印solve()数组时,它第一次完美打印并提供以下所需的输出:

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

但是当我在main()函数内再次打印时,我得到以下输出:

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

我刚刚从Python迁移到C ++,我发现它非常繁琐,有时甚至是这些。我已经尝试了对代码的各种修改,但我仍然无法弄清楚问题。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:2)

calc是一个局部变量,其生命周期从它的定义开始,并在函数退出时结束。

由于您在退出函数时返回指向它的指针,因此所述指针的解除引用将是未定义的行为(因为它后面的“对象”不再存在)。

如果你想让变量在函数返回时存活,你需要做一些动态分配它,改变:

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

为:

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

然后在完成调用后确保你delete[]