【问题标题】:Run-Time Check Failure #2 - Stack around the variable 'RL' was corrupted运行时检查失败 #2 - 变量“RL”周围的堆栈已损坏
【发布时间】:2015-02-09 21:50:22
【问题描述】:

我的问题是当我运行我的程序时,它告诉我“运行时检查失败 #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;   
}

【问题讨论】:

  • 请更改您的标题。

标签: c++ stack


【解决方案1】:

这里是:

                                    //    v---- here
for (int i = (Rth / 10); i <= Rth * 10; i = i + 0.25)

不起作用。 i 是一个整数,所以i + 0.25 在赋值中立即转换为int,所以i 不会改变。因此,循环是一个无限循环,并且随着j 在每次迭代中攀升,在某些时候它会超出您写入的数组的范围。此时,数组周围的堆栈会损坏。

这可能可以通过将i 设置为double 来解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-21
    • 2013-12-13
    • 2013-12-10
    • 2015-05-24
    • 2021-12-31
    • 2014-10-20
    • 2014-07-01
    相关资源
    最近更新 更多