C++ 取消引用具有相同地址的指针会返回不同的结果
这是我的密码:C++ 取消引用具有相同地址的指针会返回不同的结果,c++,dereference,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) + cons
#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_常量在堆栈(而不是堆)上为此数组分配内存。
当此函数返回时,此内存块可能被分配用于其他目的,因此不应在此函数之外访问。
因此,当指针返回并在以后使用时,会导致不可预知的结果
常量数组需要在main或heap中分配,所以它的生存期对于这种使用来说足够长
在这个while循环条件下
while (abs(Calc_H(Q_iter, Head, constants)) > max_error || iter_counter > 1000)
我猜,应该是ItdioTrave< 1000。
< P>代码中没有太多C++。首先,让我们删除非标准内容:然后到处都是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;
}
这只会产生未定义的行为<代码>常量是一个局部变量。当函数返回时,您在这里创建的六个元素将被销毁,但您仍保留一个指向它们的指针。C++语言不能保证会发生什么,如果你试图在以后引用那个指针(就像你那样)。如果运气好的话,程序会立即崩溃,向您显示存在严重错误,而不是生成无意义的输出
你也有点不走运,没有得到编译器的警告。如果您没有使用冗余的指针
变量,那么您可能会收到警告(至少在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
s一样安全地使用标准容器对象,而不会将原始指针的复杂性撒在代码上。谢谢,我会尝试解决这个问题。。。。再次感谢您纠正了另一个错误:)可能重复的
int main()
#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;
}
double * Calc_constants(double freq)
{
double * pointer;
double constants[6];
// ...
pointer = constants;
return pointer;
}
double * Calc_constants()
{
double constants[6];
return constants;
}
int main()
{
double* ptr = Calc_constants();
}
warning C4172: returning address of local variable or temporary