c++ - 取消引用具有相同地址的指针返回不同的结果

标签 c++ dereference

这是我的代码:

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

常量数组需要在主内存或堆上分配,因此它的生命周期足够长以适应这种用途。

在这个while循环条件下,

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

我猜,应该是iter_counter < 1000。

关于c++ - 取消引用具有相同地址的指针返回不同的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31611888/

相关文章:

c - 指针运算 :++*ptr or *ptr++?

python - 在 Python CFFI 中取消引用使用 ffi.addressof 创建的指针(C * - 等效运算符?)

c++ - 动态和静态分配的数组元素计算?

C++,如何在派生类中调用基类的重载提取运算符?

c++ - 结构定义导致我收到多重定义符号错误

c - 双指针如何真正以这种方式表现?

c - 取消引用指向不完整类型(节点)的指针

c++ - Xcode 9.4.1 中的 Apple Mach-O 链接器 (Id) 错误

c++ - 删除元素时 map 迭代器如何失效?

C - 编程数组以存储文本文档中的单词 - malloc 或取消引用问题