C++ 围绕变量";变量名";已损坏C++;
我有一个代码,应该是大学里一个项目的mips处理器的汇编程序。C++ 围绕变量";变量名";已损坏C++;,c++,C++,我有一个代码,应该是大学里一个项目的mips处理器的汇编程序。 现在我的问题是,当执行程序时,一切正常,直到程序退出主程序,它抛出以下错误: 变量二进制指令周围的堆栈已损坏 退出main时抛出错误,我在任何地方都找不到有效的解决方案 下面是我的代码示例: #include <iostream> #include <fstream> #include <string> #include <bitset> using namespace std; //
现在我的问题是,当执行程序时,一切正常,直到程序退出主程序,它抛出以下错误: 变量
二进制指令
周围的堆栈已损坏
退出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
}
我的问题是访问不存在的数组的索引 我忘了,当在C++中识别一个数组时,你会键入数组中元素的数量,而不是数组最后的索引。
使我困惑的是错误发生的地方,当用断点调试时,它会退出异常,而退出
不过,有一些解决方案,特别是使用更现代的数组类型容器,如和。出于速度优化的原因,它们也不会在默认操作模式下执行索引检查,但是有一些方法可以启用它。请参阅我的答案。这不是答案,但不能放在评论中 我们走吧
bool tobool(char x)
{
if (x == '0')
return 0;
else
return 1;
应该是
bool tobool(char x)
{
return x == '0'? false : true;
}
void register_encode(const std::string& Register, bool &address2, bool &address3, bool &address4, bool &address5, bool &address6)
int main()
然后
{
应该是
bool tobool(char x)
{
return x == '0'? false : true;
}
void register_encode(const std::string& Register, bool &address2, bool &address3, bool &address4, bool &address5, bool &address6)
int main()
{
同上
count_commas
我确信在std::string
中有一种方法可以做到这一点
也
应该是
bool tobool(char x)
{
return x == '0'? false : true;
}
void register_encode(const std::string& Register, bool &address2, bool &address3, bool &address4, bool &address5, bool &address6)
int main()
请提供一个testcase(代码示例),以便社区诊断实际问题。编写超出范围。如果不提供代码,您还想知道什么?@molbdnilo您可以指定具体位置吗?molbdnilo的意思是
bool二进制指令[31];
有31个元素,索引从0到30(不是31!)。然后,您有二进制指令[16+x]=tobool(偏移量[x]);
其中x达到15,这与(int x=0;x<代码>空main < /c> >是无效的C++。如果编译器没有告诉你,它还没有告诉你什么?尝试启用警告,而不要忽略警告。这不应该是一个答案,而是对问题的编辑。