Hello,
I feel like this is probably something silly, but for some reason if I declare a variable at a certain point in my code, then never use it... it crashes. This is literally what happens:
//... code here
int isBuf=0;//one of my variables
int zeroAddr=0;//<-- another variable I want, but never ever use (yet)
while (!file.eof())//code continues
//...
The above crashes while
//... code here
int isBuf=0;//one of myvariables
//int zeroAddr=0;//<-- another variable I want, but never ever use (yet)
while (!file.eof())//code continues
//...
Works perfectly. I have checked and zeroAddr is used absolutely nowhere else in my code.
From my understanding of C++ this should not be possible... yet its happening. Any insight as to a possible cause?
For reference here is the entire code, its purpose to emulate a custom machine language:
#include "LABASM.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <vector>
using namespace std;
string getInstructionString(int32_t x)
{
string ret="";
int imm=x&0xFFF;
int type=(x>>20)&0xF;
int a=(x>>16)&0xF;
int b=(x>>12)&0xF;
switch (type)
{
case 0:ret+="BEQ ";break;
case 1:ret+="BNE ";break;
case 2:ret+="BLT ";break;
case 3:ret+="JMP ";break;
case 4:ret+="MOV ";break;
case 5:ret+="LDA ";break;
case 6:ret+="STA ";break;
case 7:ret+="STR ";break;
case 8:ret+="INR ";break;
case 9:ret+="OUT ";break;
case 10:ret+="JBY ";break;
case 11:ret+="NAN ";break;
case 12:ret+="ADD ";break;
case 13:ret+="MUL ";break;
case 14:ret+="DIV ";break;
case 15:ret+="MOD ";break;
default:ret+="ERR ";
}
switch (a)
{
case 0:ret+="R0";break;
case 1:ret+="R1";break;
case 2:ret+="R2";break;
case 3:ret+="R3";break;
case 4:ret+="R4";break;
case 5:ret+="R5";break;
case 6:ret+="R6";break;
case 7:ret+="R7";break;
case 8:ret+="R8";break;
case 9:ret+="R9";break;
case 10:ret+="RA";break;
case 11:ret+="RB";break;
case 12:ret+="PC";break;
case 13:ret+="IR";break;
case 14:ret+="SP";break;
case 15:ret+="FP";break;
default:ret+="ER";
}
ret+=",";
switch (b)
{
case 0:ret+="R0";break;
case 1:ret+="R1";break;
case 2:ret+="R2";break;
case 3:ret+="R3";break;
case 4:ret+="R4";break;
case 5:ret+="R5";break;
case 6:ret+="R6";break;
case 7:ret+="R7";break;
case 8:ret+="R8";break;
case 9:ret+="R9";break;
case 10:ret+="RA";break;
case 11:ret+="RB";break;
case 12:ret+="PC";break;
case 13:ret+="IR";break;
case 14:ret+="SP";break;
case 15:ret+="FP";break;
default:ret+="ER";
}
ret+="(";
char buf[5];//we don't know how big our nums are... but imm<4096
sprintf(buf,"%d",imm);
ret+=buf;
ret+=")";
return ret;
}
int main(int argc, char *argv[])
{
if (argc!=2)
{
cout<<"Please specify a .lab file for processing..."<<endl;
return 0;
}
fstream ifile(argv[1]);
if (!ifile.is_open())
{
cout<<"Could not open file!"<<endl;
return 0;
}
vector<int32_t> data;
char buf[4];
int isBuf=0;
//int zeroAddr=0;
while (!ifile.eof())
{
ifile.get(buf[isBuf]);
isBuf++;
if (isBuf==4)
{
int32_t tmp=ASCIITOLABASM(buf[0]);
tmp<<=6;
tmp|=ASCIITOLABASM(buf[1]);
tmp<<=6;
tmp|=ASCIITOLABASM(buf[2]);
tmp<<=6;
tmp|=ASCIITOLABASM(buf[3]);
data.push_back(tmp);
isBuf=0;
}
}
LABASMEmulator<int32_t> e;
e.load(data,0);
int32_t ret;
int framecount=0;
do
{
cout<<endl<<getInstructionString(e[e.reg(12)])<<" @"<<e.reg(12)<<
" after "<<framecount++<<"u."<<endl;
}while (e.step(&ret));
cout<<"\nReturn value: "<<ret<<" after "<<framecount<<"u."<<endl;
return 0;
}
While the LABASM.h file defines the LABASMEmulator class as:
template <typename ASMType>
class LABASMEmulator
{// private:
size_t ASMSize;
std::map<ASMType,ASMType> mem;
ASMType *regs;
int *busy;
std::fstream vdisk;
public:
LABASMEmulator();
~LABASMEmulator();
bool step(ASMType *);//returns false on halt
void load(std::vector<ASMType>,ASMType);
ASMType ®(int);//reg access
ASMType &operator[](ASMType);//mem access
};
Of course it has guards, a few preprocessors, and a source cpp file with it, but I do not believe any of these could be causing the issue.
At this point my best guess is http://xkcd.com/1316/
EDIT: I thought I would add why I want that variable, it is to allow for specifying the second argument of LABASMEmulator::load based on input. When that wasn't working I removed 1 statement of code at a time until all that was left was the declaration itself... thus I isolated my issue, however I still don't know why it causes it to fail in the first place.