具有相同地址的指针解引用返回不同的结果
Dereferencing pointer with same adress returns different result
我的代码:
#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::array
或std::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
一样安全地使用标准容器对象,而不必在代码中到处都是原始指针的复杂性。
- 如何通过引用返回对象
- 函数如何使用引用返回所需的数字?
- 如何防止引用返回的私有结构的突变
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 通过引用返回的变量的范围
- 对于具有引用返回类型的搜索算法,默认返回值应该是什么?
- 运算符重载C++类中的引用返回
- C++对象引用返回不同的值
- 解释通过从函数引用返回数组的语法
- 具有引用返回类型的重写方法上的协变返回类型无效
- 为什么在通过引用返回运算符分配时取消引用'this'指针?
- 为什么我在函数中使用引用并通过引用返回它仍然有效?
- 直接在 C++ 中将值分配给引用返回类型
- C++当您取消引用指向类对象的指针,然后将其作为引用返回时,是否可以对此引用调用方法
- 可以通过常量引用返回默认参数的值吗?
- 按值与右值引用返回
- 非常量引用返回函数在常量值返回函数上用作 r 值
- 当我使用按引用返回时,我不知道这些代码之间的区别
- 为什么通过引用返回向量比通过移动返回要快得多?
- 通过引用返回包含对象的向量