我正在使用C ++创建一个虚拟机,一切都在OSX上运行良好,但是当我在Windows上测试它时编译很好,没有错误或警告,但程序在我尝试为索引赋值时保持崩溃在数组中。我没有使用任何外部库或任何东西,所以我不知道它为什么会一直崩溃。
THE LINE WHERE IT CRASHES:
instarray[i] = value;
下面我还提供了整个文件来帮助您调试它。
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#define OP_EOI 0
#define OP_EOP 1
#define OP_PUSH 2
#define OP_POP 3
#define OP_PRINT 4
#define OP_ADD 5
#define OP_MUL 6
#define OP_SUB 7
using namespace std;
class reedoovm {
private:
string filedata;
string instruction;
string file;
int instr;
int instructionCount;
int instructionPointer;
int stack;
public:
string load_program(string filename) {
ifstream rdfile(filename);
while(rdfile >> instruction) { /* Get each instruction */
filedata += instruction; /* Append the instruction to filedata */
filedata += ","; /* Append a comma to separate each instruction */
}
rdfile.close(); /* Close the file */
return filedata; /* Return the filedata */
}
int *instrToArr(string file) {
stringstream hextoint;
unsigned int value;
string s = file; /* store fconv in a variable "s" */
string delimiter = ","; /* The delimiter */
size_t pos = 0;
string token;
int i = 0;
int inst;
static int* instarray;
cout << "works" << endl;
//instarray = (int*) calloc(instructionCount,sizeof(int));
instarray = (int *) malloc(instructionCount*sizeof(int));
// instarray[10];
instructionCount = count(file.begin(), file.end(), ',');
while ((pos = s.find(delimiter)) != string::npos) { /* Convert hex instructions to decimal */
cout << "works" << endl;
token = s.substr(0, pos);
cout << token << endl;
stringstream hextoint(token);
hextoint >> hex >> value;
cout << value << endl;
if (i < instructionCount) {
instarray[i] = value;
i++;
cout << "works" << endl;
}
cout << i << endl;
s.erase(0, pos + delimiter.length());
}
return instarray;
}
int * instructionArray(int instructionArray[]) {
return instructionArray;
}
int getNextIntruction(int instructions[], int i) {
return instructions[i];
}
void do_PRINT() {
}
void do_PUSH(int instructions, int i) {
//cout << instructions[i + 1] << endl;
}
void run_program(int instructions[], string file) {
int loop = 1;
int i = 0;
string delimiter = ","; /* The delimiter */
size_t pos = 0;
string token;
int iterator = 0;
instructionCount = count(file.begin(), file.end(), ',');
int instructionOrLiteralArray[instructionCount];
while ((pos = file.find(delimiter)) != string::npos) { /* Convert hex instructions to decimal */
token = file.substr(0, pos);
if (token.length() == 2) { /* Operation */
instructionOrLiteralArray[iterator] = 0;
} else {
instructionOrLiteralArray[iterator] = 1; /* Literal */
}
iterator++;
file.erase(0, pos + delimiter.length());
}
while (loop) {
instr = getNextIntruction(instructions, i);
if (instr == OP_EOI && instructionOrLiteralArray[i] == 0) {
cout << "EOI" << endl;
} else if (instr == OP_EOI && instructionOrLiteralArray[i] == 1) {
cout << "Literal" << endl;
}
if (instr == OP_PUSH && instructionOrLiteralArray[i] == 0) {
do_PUSH(instr, i);
} else if (instr == OP_PUSH && instructionOrLiteralArray[i] == 1) {
cout << "Literal" << endl;
}
if (instr == OP_PRINT && instructionOrLiteralArray[i] == 0) {
do_PRINT();
} else if (instr == OP_PRINT && instructionOrLiteralArray[i] == 1) {
cout << "Literal" << endl;
}
if (instr == OP_POP && instructionOrLiteralArray[i] == 0) {
cout << "POP" << endl;
} else if (instr == OP_POP && instructionOrLiteralArray[i] == 1) {
cout << "Literal" << endl;
}
if (instr == OP_ADD && instructionOrLiteralArray[i] == 0) {
cout << "ADD" << endl;
} else if (instr == OP_ADD && instructionOrLiteralArray[i] == 1) {
cout << "Literal" << endl;
}
if (instr == OP_SUB && instructionOrLiteralArray[i] == 0) {
cout << "MUL" << endl;
} else if (instr == OP_MUL && instructionOrLiteralArray[i] == 1) {
cout << "Literal" << endl;
}
else if (instructionOrLiteralArray[i] == 1) {
cout << "Literal" << endl;
}
if (i < instructionCount) {
i++;
} else {
loop = 0;
}
}
}
void execute_program(string s) {
file = load_program(s);
int * arr = instrToArr(file);
int * instructions = instructionArray(arr);
run_program(instructions, file);
}
};
int main(int argc, char* argv[]) {
reedoovm rd;
rd.execute_program(argv[1]);
return 0;
}
我一直盯着代码看了一个多小时,我似乎无法找到问题。
答案 0 :(得分:0)
该函数具有未定义的行为
instarray = (int *) malloc(instructionCount*sizeof(int));
// instarray[10];
instructionCount = count(file.begin(), file.end(), ',');
您正在使用未初始化的变量instructionCount
,或者至少我找不到在分配内存的语句中初始化它的代码。只有在那之后你才能分配变量。
另请注意,您应该在C ++中使用operator new
而不是malloc
。
答案 1 :(得分:0)
这通常意味着你超越了数组的末尾,并且它导致了分段错误。请记住,数组是0索引的(例如,如果数组有3个元素,则有效索引值的范围为0到2)。看起来你在代码中占了这个,但是你仍然以某种方式超出了数组的范围。我会添加一个检查以确保instructionCount不大于sizeof(instarray) - 1,并且它大于0.您可以使用调试器和/或cout语句,查看这些变量的值以查看是什么出错了。