basic_string::_S_create

am trying to run mu program but i got the following error message
"terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create"
I understand that this might be issue with the memory and maybe is where i need to make a change but i still cant find the issue. Can someone help me pls?

main.cpp

using namespace std;
ifstream input;
ofstream output;
int Registers[32] = {};
int Cycle = 0;

//print details which change

void print_memory(int t[], int a[], int m)
{
output << endl << "Memory State:" << endl;
for (int i = 0; i < m; i++)
{
output << "[" << a[i] << "]: " << t[i] << endl;
}
}

//print registers
void print()
{
//
}
void print_final_state_text() {
//
}
void print_final_state() {
//
}


//find destination adress

int find(int arr[], int SIZE, int values) {
for (int a = 0; a < 40000; a++)
{
if (arr[a] == values)
{
return a;
}
}
}

//find register position
int table(string x) {
int i = 0;
if (x == "zero" || x == "zero " || x == " zero" || x == " zero ")
{
i = 0;
}
else if (x == "at" || x == "at " || x == " at" || x == " at ")
{
i = 1;
}
else if (x == "v0" || x == "v0 " || x == " v0" || x == " v0 ") {
i = 2;
}
else if (x == "v1" || x == "v1 " || x == " v1" || x == " v1 ")
{
i = 3;
}
..
}
else if (x == "ra" || x == "ra " || x == " ra" || x == " ra ")
{
i = 31;
}
return i;
}
void printtofile(string k) {
output << k;
}

void print_cycle(int x) {

output << "-Cycle N" << x << "-" << endl;

}


int main() {

int address[40000] = {};
int timi[40000] = {};
Registers[28] = 268468224;
Registers[29] = 2147483644;
//sam var
int ee = 0;
int aa = 0;
int kk = 0;
int kkk = 0;
int pccounter = 0;
//-INPUT CYCLES FROM THE USER AND PRINT MESSAGES-

int pc;
string label;
string opcode_g;
string rs_g;
string rt_g;
string rd_g;
string imm_hex;
struct instruction* next;
};


string k;
char ch;
struct instruction* header;
struct instruction* pointer;
struct instruction* pointerprevious;
header = new struct instruction;
pointer = header;

struct instruction IF;
struct instruction ID;
..


input.open("test");
output.open("pipeline");


int io = 0, t = 0;
while (!(input.eof())) {
ch = input.get();
replace(k.begin(), k.end(), '(', ' ');
replace(k.begin(), k.end(), ')', ' ');
if (ch == 'b') {
tt++;
}
else if (ch == '.') {
while (ch != '\n')
{
ch = input.get();
}
continue;
}
else if (ch == '#') {
while (ch != '\n') {
ch = input.get();
if (input.eof()) {
break;
}
}
}
else if (ch == '$') {
continue;
}
else if (ch == '\t') {
continue;
}
else if (ch == ':') {
string label(k.begin(), find(k.begin(), k.end(), ':'));
k += ch;
ch = input.get();
while ((ch == '\n') || (ch == '\t')) {
ch = input.get();
}
}
else if (ch == ',') {
continue;
}
if (ch == '\n' || input.eof()) {
if (k != "") {
size_t search = k.find(':');
if (search != k.npos) {
string label(k.begin(), find(k.begin(), k.end(), ':'));
pointer->label = label;
string temp(find(k.begin(), k.end(), ':') + 1, k.end());
string opcode1(temp.begin(), find(temp.begin(), temp.end(), ' '));
string temp2(find(k.begin(), k.end(), ' ') + 1, k.end());
string rd1(temp2.begin(), find(temp2.begin(), temp2.end(), ' '));
string temp3(find(temp2.begin(), temp2.end(), ' ') + 1, temp2.end());
string rt1(temp3.begin(), find(temp3.begin(), temp3.end(), ' '));
string temp4(find(temp3.begin(), temp3.end(), ' ') + 1, temp3.end());
string rs1(temp4.begin(), find(temp4.begin(), temp4.end(), '\n'));
pointer->opcode_g = opcode1;
pointer->rd_g = rd1;
pointer->rt_g = rt1;
pointer->rs_g = rs1;
pointer->pc = pccounter;
pccounter += 4;
}
else {
pointer->label = "";
string opcode_g(k.begin(), find(k.begin(), k.end(), ' '));
string temp(find(k.begin(), k.end(), ' ') + 1, k.end());
string rd_g(temp.begin(), find(temp.begin(), temp.end(), ' '));
string temp3(find(temp.begin(), temp.end(), ' ') + 1, temp.end());
string rt_g(temp3.begin(), find(temp3.begin(), temp3.end(), ' '));
string temp4(find(temp3.begin(), temp3.end(), ' ') + 1, temp3.end());
string rs_g(temp4.begin(), find(temp4.begin(), temp4.end(), '\n'));
pointer->opcode_g = opcode_g;
pointer->rd_g = rd_g;
pointer->rt_g = rt_g;
pointer->rs_g = rs_g;
pointer->pc = pccounter;
pccounter += 4;
}
pointerprevious = pointer;
pointer = new struct instruction;
pointerprevious->next = pointer;
pointer->next = NULL;
k = "";
}
while (ch == '\n') {
ch = input.get();
}
if (ch == '\t') {
continue;
}
}
k += ch;
}
//---------
pointer = header;
IF.opcode_g = pointer->opcode_g;
IF.rd_g = pointer->rd_g;
IF.rt_g = pointer->rt_g;
IF.rs_g = pointer->rs_g;
output << "//" << endl;

while (pointer != NULL) {


//-IF-
IF.opcode_g = pointer->opcode_g;
IF.rd_g = pointer->rd_g;
IF.rt_g = pointer->rt_g;
IF.rs_g = pointer->rs_g;

if ((IF.opcode_g == "add" || IF.opcode_g == "addi" || IF.opcode_g == "addiu" || IF.opcode_g == "addu" || IF.opcode_g == "and" || IF.opcode_g == "andi" || IF.opcode_g == "nor" || IF.opcode_g == "or" || IF.opcode_g == "ori" || IF.opcode_g == "slt" || IF.opcode_g == "slti" || IF.opcode_g == "sltiu" || IF.opcode_g == "sltu" || IF.opcode_g == "sll" || IF.opcode_g == "srl" || IF.opcode_g == "sub" || IF.opcode_g == "subu"
|| IF.opcode_g == "beq" || IF.opcode_g == "bne" || IF.opcode_g == "lw" || IF.opcode_g == "sw")) {


}
//-ID-
if ((ID.opcode_g == "add" || ID.opcode_g == "addi" || ID.opcode_g == "addiu" || ID.opcode_g == "addu" || ID.opcode_g == "and" || ID.opcode_g == "andi" || ID.opcode_g == "nor" || ID.opcode_g == "or" || ID.opcode_g == "ori" || ID.opcode_g == "slt" || ID.opcode_g == "slti" || ID.opcode_g == "sltiu" || ID.opcode_g == "sltu" || ID.opcode_g == "sll" || ID.opcode_g == "srl" || ID.opcode_g == "sub" || ID.opcode_g == "subu"
|| ID.opcode_g == "beq" || ID.opcode_g == "bne" || ID.opcode_g == "lw" || ID.opcode_g == "sw")) {


}
//-EX-
if (EX.opcode_g == "add" || EX.opcode_g == "addu" || EX.opcode_g == "and" || EX.opcode_g == "nor" || EX.opcode_g == "or" || EX.opcode_g == "slt" || EX.opcode_g == "sltu" || EX.opcode_g == "sll" || EX.opcode_g == "srl" || EX.opcode_g == "sub" || EX.opcode_g == "subu") {
y = table(EX.rd_g);
u = table(EX.rt_g);
p = table(EX.rs_g);
if (EX.opcode_g == "add" || EX.opcode_g == "addu") {
AluOut_ex = (Registers[u] + Registers[p]);
}
else if (EX.opcode_g == "and") {
AluOut_ex = (Registers[u] & Registers[p]);
}
else if (EX.opcode_g == "nor") {
AluOut_ex = (Registers[u] | Registers[p]);
AluOut_ex = ~AluOut_ex;
}
else if (EX.opcode_g == "or") {
AluOut_ex = (Registers[u] | Registers[p]);
}
else if (EX.opcode_g == "slt") {
AluOut_ex = 0;
if (Registers[u] < Registers[p]) {
AluOut_ex = 1;
}
else {
AluOut_ex = 0;
}
}
else if (EX.opcode_g == "sltu") {
AluOut_ex = 0;
if ((unsigned)Registers[u] < (unsigned)Registers[p]) {
AluOut_ex = 1;
}
else {
AluOut_ex = 0;
}
}
else if (EX.opcode_g == "sll") {
int h = 0;
stringstream ss(EX.rs_g);
ss >> h;
AluOut_ex = ((Registers[u]) * (pow(2, h)));
}
..
print_memory(timi , address , ee );

}




print_final_state_text();
output << "PC = " << pccounter - 4 << endl;
print_final_state();
print_memory(timi , address , ee );
input.close();
output.close();
return (0);
}
An issue could be this:

string temp(find(k.begin(), k.end(), ':') + 1, k.end());

What happens when find(....) return k.end() and you add 1?
You have this problem at several places.
I can't compile your code, even after several minutes of fixing syntax errors and creating dummy structures for the ones you haven't defined. Please post code something that will compile.
i cant put the whole code because its too big, is there any way i can show you what i have done?
am trying to run mu program but i got the following error message
"terminate called after throwing an instance of 'std::length_error'
what(): basic_string::_S_create"

Did you run the program with your debugger? The debugger should be able to tell you exactly where it detected the problem, and you should be able to view the contents of the variables at the time of the crash.

Edit:
i cant put the whole code because its too big, is there any way i can show you what i have done?

Then post the smallest possible complete program that illustrates the problem.
Last edited on
Topic archived. No new replies allowed.