运行时检查失败 # 2 - 变量周围的堆栈'ary'已损坏。为什么?

Run Time Check failure # 2 - Stack around variable 'ary' was corrupted. Why?

本文关键字:ary 已损坏 为什么 堆栈 失败 检查 周围 变量 运行时      更新时间:2023-10-16

我是新来的,不知道我在做什么。编译警告已打开,不显示任何警告。可执行文件弹出并发出运行时检查失败 #2 的警报。

对于

为什么会发生这种情况,我们将不胜感激。

#include <iostream> 
#include <string>
using namespace std;
class romanType {
public:
    string strg;
    void inputRoman(int ary[]);
    //void CalculateRoman(int ary[]);
    //void outputRoman(int total);
};

int main()
{
    int M = 1000;
    int D = 500;
    int C = 100;
    int L = 50;
    int X = 10;
    int V = 5;
    int I = 1;

    romanType numerals; 
    int ary[50];
    cout << "This is to convert your input of Roman numerals to a positiver integer" << endl;
    cout << "When prompted, do as you're told" << endl;

    numerals.inputRoman(&ary[50]);
//  numerals.CalculateRoman(&input[50]);

    return 0; 
}   
void romanType::inputRoman(int ary[])
{
    string strg;
    int array_size;
    int i;
    cout << "Input the an appropriate Roman Numeral value" << endl;
    cin >> strg;
    array_size = strg.length();
    for (i = 0; i < array_size; i++)
    {
        ary[i] = strg[i];
    }

}
    /*
void romanType::CalculateRoman(int ary[])
{
    int total = 0;
    int i;
    for (i=0; i < 50 ; i++){
    if (ary[i] < (ary[i + 1])){
        total = total + (ary[i + 1] - ary[i]);
    }
    else {
        total = total + ary[i];
    }
    }
    cout << "Your conversion should equal " << total << endl;
}
*/`

>&ary[50]ary的第51个元素的地址,这意味着它指向ary的最后一个元素之后。将其更改为ary

numerals.inputRoman(ary);