具有相同地址的指针解引用返回不同的结果

Dereferencing pointer with same adress returns different result

本文关键字:引用 返回 结果 指针 地址      更新时间:2023-10-16

我的代码:

#include "stdafx.h"
#include "math.h"
#include <iostream>
using namespace std;
double Calc_H(double Q, double Head, double *constants)
{
    return (constants[0] * pow(Q, 4) + constants[1] * pow(Q, 3) + constants[2] * pow(Q, 2) + constants[3] * Q + constants[4] - Head);
}
double Calc_dH(double Q, double *constants)
{
    return (4 * constants[0] * pow(Q, 3) + 3 * constants[1] * pow(Q, 2) + 2 * constants[2] * Q + constants[3]);
}
double NewtonRaphson(double Head, double first_guess, double max_error, double * constants)
{
    double Q_iter = first_guess;
    int iter_counter = 1;
    cout << constants << endl << constants[0] << endl << constants[1] << endl;
    while (abs(Calc_H(Q_iter, Head, constants)) > max_error || iter_counter > 1000)
    {
        Q_iter = Q_iter - Calc_H(Q_iter, Head, constants) / Calc_dH(Q_iter, constants);
        iter_counter++;
    }
    return Q_iter;
}
double * Calc_constants(double freq)
{
    double * pointer;
    double constants[6];
    constants[0] = -1.2363 + 2.3490 / 10 * freq - 1.3754 / 100 * pow(freq, 2) + 2.9027 / 10000 * pow(freq, 3) - 2.0004 / 1000000 * pow(freq, 4);
    constants[1] = 1.9547 - 4.5413 / 10 * freq + 3.5392 / 100 * pow(freq, 2) - 8.1716 / 10000 * pow(freq, 3) + 5.9227 / 1000000 * pow(freq, 4);
    constants[2] = -5.3522 - 4.5413 / 10 * freq - 1.3311 / 100 * pow(freq, 2) + 4.8787 / 10000 * pow(freq, 3) - 4.8767 / 1000000 * pow(freq, 4);
    constants[3] =  3.8894 / 100 + 3.5888 / 10 * freq + 1.0024 / 100 * pow(freq, 2) - 5.6565 / 10000 * pow(freq, 3) + 7.5172 / 1000000 * pow(freq, 4);
    constants[4] = -8.1649 + 5.4525 / 10 * freq - 3.2415 / 100 * pow(freq, 2) + 8.9033 / 10000 * pow(freq, 3) - 9.0927 / 1000000 * pow(freq, 4);
    constants[5] =  2.1180 / 10 + 5.0018 / 100 * freq + 6.0490 / 1000 * pow(freq, 2) - 1.5707 / 100000 * pow(freq, 3) + 3.7572 / 10000000 * pow(freq, 4);
    pointer = constants;
    return pointer;
}
int _tmain(int argc, _TCHAR* argv[])
{
    double * constants;
    //Determine constants based on freq (see manual pump)
    double freq;
    cin >> freq; 
    double head;
    cin >> head;
    constants = Calc_constants(freq);
    cout << constants[0] << endl << constants[1] << endl << constants << endl;
    cout << NewtonRaphson(head, 0, 0.001, constants) << endl;
    cin >> freq;    
    return 0;
}

函数Calc_constants返回一个指向计算值数组的指针。到目前为止一切顺利。

函数NewtonRaphson接受指向该数组的指针作为参数。当在这个函数中解引用这个指针时,它为constants[0]constants[1]返回不同的结果。我觉得这很奇怪,因为指针"指向"的地址是相同的。

为了澄清这是输出(cout):

-0.09505
2.6008
OOD6F604
00D6F604
-9.25596e+0.61
-9.25596e+0.61
-1.08038e-0.62
double * Calc_constants(double freq)
{
    double * pointer;
    double constants[6];

Calc_constants为这个数组在它的堆栈上分配内存,而不是在堆上。当此函数返回时,该内存块可能被分配用于其他目的,因此不应该在此函数之外访问。因此,当指针返回并在以后使用时,会导致不可预测的结果。

constants数组需要在main或heap中分配,这样它的生命周期才足够长。

在这个while循环条件中,

while (abs(Calc_H(Q_iter, Head, constants)) > max_error || iter_counter > 1000)

应该是iter_counter <1000 .

您的代码中没有太多c++。首先,让我们删除非标准的内容:

#include "stdafx.h"
#include "math.h"
#include <iostream>

应该成为:

#include <math.h>
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])

应该成为:

int main()

然后到处都是c风格的数组。你不会想这么做的。使用std::arraystd::vector,问题将自行消失。

以下是std::vector的示例:

#include <math.h>
#include <iostream>
#include <vector>
double Calc_H(double Q, double Head, std::vector<double> const& constants)
{
    return (constants[0] * pow(Q, 4) + constants[1] * pow(Q, 3) + constants[2] * pow(Q, 2) + constants[3] * Q + constants[4] - Head);
}
double Calc_dH(double Q, std::vector<double> const& constants)
{
    return (4 * constants[0] * pow(Q, 3) + 3 * constants[1] * pow(Q, 2) + 2 * constants[2] * Q + constants[3]);
}
double NewtonRaphson(double Head, double first_guess, double max_error, std::vector<double> const& constants)
{
    double Q_iter = first_guess;
    int iter_counter = 1;
    std::cout << constants.data() << std::endl << constants[0] << std::endl << constants[1] << std::endl;
    while (abs(Calc_H(Q_iter, Head, constants)) > max_error && iter_counter < 1000)
    {
        Q_iter = Q_iter - Calc_H(Q_iter, Head, constants) / Calc_dH(Q_iter, constants);
        iter_counter++;
    }
    return Q_iter;
}
std::vector<double> Calc_constants(double freq)
{
    std::vector<double> constants(6);
    constants[0] = -1.2363 + 2.3490 / 10 * freq - 1.3754 / 100 * pow(freq, 2) + 2.9027 / 10000 * pow(freq, 3) - 2.0004 / 1000000 * pow(freq, 4);
    constants[1] = 1.9547 - 4.5413 / 10 * freq + 3.5392 / 100 * pow(freq, 2) - 8.1716 / 10000 * pow(freq, 3) + 5.9227 / 1000000 * pow(freq, 4);
    constants[2] = -5.3522 - 4.5413 / 10 * freq - 1.3311 / 100 * pow(freq, 2) + 4.8787 / 10000 * pow(freq, 3) - 4.8767 / 1000000 * pow(freq, 4);
    constants[3] =  3.8894 / 100 + 3.5888 / 10 * freq + 1.0024 / 100 * pow(freq, 2) - 5.6565 / 10000 * pow(freq, 3) + 7.5172 / 1000000 * pow(freq, 4);
    constants[4] = -8.1649 + 5.4525 / 10 * freq - 3.2415 / 100 * pow(freq, 2) + 8.9033 / 10000 * pow(freq, 3) - 9.0927 / 1000000 * pow(freq, 4);
    constants[5] =  2.1180 / 10 + 5.0018 / 100 * freq + 6.0490 / 1000 * pow(freq, 2) - 1.5707 / 100000 * pow(freq, 3) + 3.7572 / 10000000 * pow(freq, 4);
    return constants;
}
int main()
{
    //Determine constants based on freq (see manual pump)
    double freq;
    std::cin >> freq; 
    double head;
    std::cin >> head;
    std::vector<double> constants = Calc_constants(freq);
    std::cout << constants[0] << std::endl << constants[1] << std::endl << constants.data() << std::endl;
    std::cout << NewtonRaphson(head, 0, 0.001, constants) << std::endl;
    std::cin >> freq;    
    return 0;
}

(我也修改了while循环,我猜是你想要的。)

可以看到,元素访问与C数组具有相同的语法。指向std::vector封装的数据的指针通过data()获得。我已经添加了这个,因为你的原始代码打印数组的地址;对于这类应用程序,在实际代码中很少需要data()


现在,就你的原始代码而言:

double * Calc_constants(double freq)
{
    double * pointer;
    double constants[6];
    // ...    
    pointer = constants;
    return pointer;
}

这只会产生未定义行为constants是一个局部变量。当函数返回时,您在这里创建的六个元素将被销毁,但您保留了指向它们的指针。如果您稍后尝试解引用该指针(就像您所做的那样),c++语言不保证会发生什么。如果运气好的话,程序会立即崩溃,向您显示有一个严重的错误,而不是生成无意义的输出。

你也有点不幸,没有得到编译器的警告。如果你没有使用多余的pointer变量,那么你可能会收到一个警告(至少在VC 2013中)。

简单的例子:

double * Calc_constants()
{
    double constants[6];
    return constants;
}
int main()
{
    double* ptr = Calc_constants();
}

VC 2013警告:

warning C4172: returning address of local variable or temporary

对于std::vector,数据是在内部分配的,这样您就可以安全地返回对象。您可以像使用简单的int一样安全地使用标准容器对象,而不必在代码中到处都是原始指针的复杂性。