c++ - 变量 "variable name"周围的堆栈已损坏 C++

标签 c++

我有一个代码,应该是大学项目的 mips 处理器的汇编程序。
现在我的问题是在执行程序时一切正常,直到程序退出 main 它抛出这个错误:

stack around the variable binaryinstruction was corrupted .

退出 main 时抛出错误,我在任何地方都找不到有效的解决方案。

下面是我的代码示例:

#include <iostream>
#include <fstream>
#include <string>
#include <bitset>
using namespace std;
//function to convert string to binary
bool tobool(char x)
{
    if (x == '0')
        return 0;
    else
        return 1;
}
//function to decode the register name into an address using call by refrence to modify the instruction boolean encoding array
void register_encode(string Register, bool &address2, bool &address3, bool &address4, bool &address5, bool &address6)
{
    if (Register == "$zero")
    {
        address2 = 0;   address3 = 0;   address4 = 0;   address5 = 0;   address6 = 0;
    }
    else if (Register == "$t3")
    {
        address2 = 0; address3 = 1; address4 = 0; address5 = 1; address6 = 1;
    }
    else if (Register == "$s2")
    {
        address2 = 1; address3 = 0; address4 = 0; address5 = 1; address6 = 0;
    }
}

//function to count the number of occurence of commas (,) inside the string
int count_commas(string s) {
    int count = 0;

    for (int i = 0; i < s.size(); i++)
        if (s[i] == ',') count++;
    return count;
}

void main()
{
    int numberofinstruction = 0;
    string testingstring;
    string line[64];
    ifstream myfile;
    myfile.open("test.txt");
    // taking input from file into an array "line" which holds every single line inside the array in a single element inside the array
    while (getline(myfile, testingstring))
    {

        line[numberofinstruction] = testingstring;
        numberofinstruction++;
    }
    myfile.close();
    //transform to binary
    bool binaryinstruction[31];
    string firstterm, secondterm, thirdterm, fourthterm;
    for (int counter = 0; counter<numberofinstruction; counter++)
    {
        switch (count_commas(line[counter])) {
        case 1:
            firstterm = line[counter].substr(0, line[counter].find(" "));
            secondterm = line[counter].substr(line[counter].find(" ") + 1, line[counter].find(",") - line[counter].find(" ") - 1);
            thirdterm = line[counter].substr(line[counter].find(",") + 1, line[counter].size() - line[counter].find(","));
            if (firstterm == "lw")
            {   //LW encoding
                binaryinstruction[0] = 1;
                binaryinstruction[1] = 0;
                binaryinstruction[2] = 0;
                binaryinstruction[3] = 0;
                binaryinstruction[4] = 1;
                binaryinstruction[5] = 1;
                register_encode(secondterm, binaryinstruction[11], binaryinstruction[12], binaryinstruction[13], binaryinstruction[14], binaryinstruction[15]);
                string offset = thirdterm.substr(0, thirdterm.find('('));
                offset = bitset<16>(stoi(offset)).to_string();
                for (int x = 0; x <= 15; x++)
                {
                    binaryinstruction[16 + x] = tobool(offset[x]);
                }
                string lwregister = thirdterm.substr(thirdterm.find('$'), thirdterm.size() - thirdterm.find('(') - 2);
                register_encode(lwregister, binaryinstruction[6], binaryinstruction[7], binaryinstruction[8], binaryinstruction[9], binaryinstruction[10]);
            }
            else
            {   //SW encoding
                binaryinstruction[0] = 1;
                binaryinstruction[1] = 0;
                binaryinstruction[2] = 1;
                binaryinstruction[3] = 0;
                binaryinstruction[4] = 1;
                binaryinstruction[5] = 1;
                //begin
                register_encode(secondterm, binaryinstruction[11], binaryinstruction[12], binaryinstruction[13], binaryinstruction[14], binaryinstruction[15]);
                string offset = thirdterm.substr(0, thirdterm.find('('));
                offset = bitset<16>(stoi(offset)).to_string();
                for (int x = 0; x <= 15; x++)
                {
                    binaryinstruction[16 + x] = tobool(offset[x]);
                }
                string lwregister = thirdterm.substr(thirdterm.find('$'), thirdterm.size() - thirdterm.find('(') - 2);
                register_encode(lwregister, binaryinstruction[6], binaryinstruction[7], binaryinstruction[8], binaryinstruction[9], binaryinstruction[10]);
                //end

            }
            break;

        }
    }
    //testing
    ofstream myfile2("testout.txt");

    for (int f = 0; f <= 31; f = f + 8)
    {
        myfile2 << binaryinstruction[f] << binaryinstruction[f + 1] << binaryinstruction[f + 2] << binaryinstruction[f + 3] << binaryinstruction[f + 4] << binaryinstruction[f + 5] << binaryinstruction[f + 6] << binaryinstruction[f + 7] << endl;
    }
    //end testing
}

在测试文件中,我有 1 行是 lw $t3,4($s2),要写入测试文件的输出与预期的一样:

10001110
01001011
00000000
00000100

最佳答案

我的问题是访问不存在的数组索引 我忘记了在 C++ 中识别数组时输入的是数组中元素的数量,而不是数组的最后一个索引。 让我困惑的是抛出错误的地方,当使用断点调试时,它会在退出 void main() 时抛出异常,而不是在访问未识别的数组元素时抛出异常。

关于c++ - 变量 "variable name"周围的堆栈已损坏 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34464244/

相关文章:

c++ - 捕获在 Windows 上显示动画系统光标的哪一步

c++ - CreateSymbolicLink 相当于 “mklink/J” ?

c++ - _CrtCheckMemory 的可靠性如何?

c++ - 使用 C++ 遗留驱动程序的 mongodb 查询导致 BSONElement : bad type -64

c++ - 多线程独立任务

C++ 指针容器

c# - 如何在 C++ 中复制包含空终止符的内存块

c++ - 从 dll 中获取 DLL 的名称

c++ - CUDA并行计算加速体积计算

c++ - eclipse-cdt中如何自动生成函数头注释?