我的问题是,当我运行我的程序时,它告诉我“运行时检查失败 #2 - 变量‘RL’周围的堆栈已损坏。”
#include<iostream>
double current(double Eth, double Rth, double RL[], int j);
double power(double I[], int k, double RL[], int j);
int main()
{
using namespace std;
double Eth, Rth, RL[100], I[100], P[100], Pmax;
Pmax = 0;
cout << "enter the values of Eth and Rth respectively " << endl;
cin >> Eth >> Rth;
int j = 0;
for (int i = (Rth / 10); i <= Rth * 10; i = i + 0.25)
{
RL[j] = i;
I[j] = current(Eth, Rth, RL, j);
P[j] = power(I, j, RL, j);
if (P[j]> Pmax)
Pmax = P[j];
j++;
}
cout << " the max power =" << Pmax << endl;
return 0;
}
double current(double Eth, double Rth, double RL[], int j)
{
double IL;
IL = (Eth / (RL[j] * Rth));
return IL;
}
double power(double I[], int k, double RL[], int j)
{
double Pow;
Pow = I[k] * I[k] * RL[j];
return Pow;
}
最佳答案
这里是:
// v---- here
for (int i = (Rth / 10); i <= Rth * 10; i = i + 0.25)
不起作用。 i
是一个整数,所以i + 0.25
在赋值时会立即转换为int
,所以i
不会改变。因此,该循环是一个无限循环,随着 j
在每次迭代中攀升,在某些时候它会超出您写入的数组的边界。那时,阵列周围的堆栈已损坏。
可能这可以通过将 i
设为 double
来解决。
关于c++ - 运行时检查失败 #2 - 变量 'RL' 周围的堆栈已损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27422395/