Trouble figuring out what's wrong with this code on Linux - c++

I'm having trouble figuring out why my code is not working on linux. I'm getting errors with the shared_ptrs like: (Partial List)
roject.cpp:21: error: ISO C++ forbids declaration of 'shared_ptr' with no type
project.cpp:21: error: expected ';' before '<' token
project.cpp:22: error: ISO C++ forbids declaration of 'shared_ptr' with no type
project.cpp:22: error: expected ';' before '<' token
project.cpp:23: error: ISO C++ forbids declaration of 'shared_ptr' with no type
project.cpp:23: error: expected ';' before '<' token
project.cpp:30: error: ISO C++ forbids declaration of 'shared_ptr' with no type
project.cpp:30: error: expected ';' before '<' token
project.cpp:37: error: ISO C++ forbids declaration of 'shared_ptr' with no type
project.cpp:37: error: expected ';' before '<' token
project.cpp:82: error: 'shared_ptr' has not been declared
project.cpp:82: error: expected ',' or '...' before '<' token
project.cpp:153: error: 'shared_ptr' has not been declared
project.cpp:153: error: expected ',' or '...' before '<' token
project.cpp: In member function 'void device_table::addCore(int)':
project.cpp:86: error: 'process' was not declared in this scope
project.cpp:88: error: 'struct device_item' has no member named 'process'
project.cpp:89: error: 'currentTime' was not declared in this scope
project.cpp:92: error: 'struct device_item' has no member named 'process'
project.cpp:98: error: 'struct device_item' has no member named 'process'
project.cpp:104: error: 'struct device_item' has no member named 'process'
project.cpp:110: error: 'struct device_item' has no member named 'process'
project.cpp:124: error: 'process_state' is not a class or namespace
project.cpp:130: error: 'process_state' is not a class or namespace
project.cpp:137: error: 'struct device_item' has no member named 'process'
project.cpp:137: error: 'process' was not declared in this scope
project.cpp:138: error: 'currentTime' was not declared in this scope
project.cpp:140: error: 'process_state' is not a class or namespace
project.cpp: In member function 'void device_table::addDisk(int)':
project.cpp:156: error: 'struct device_item' has no member named 'process'
project.cpp:156: error: 'disk' was not declared in this scope
project.cpp:157: error: 'currentTime' was not declared in this scope
project.cpp:167: error: 'process_state' is not a class or namespace
project.cpp: In member function 'void device_table::checkCoreAndDisk(int)':
Here's my code. I realized I shouldn't have used vectors. There's always a next time.
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <memory>
#include <algorithm>
#include <queue>
using namespace std;
enum process_state { P_NONE, READY, RUNNING, WAITING };
enum event_component { E_NONE, START, CORE, TTY, DISK };
struct data_entry {
string text;
int time;
};
struct process_entry {
int process;
shared_ptr<data_entry> * dataStart;
shared_ptr<data_entry> * dataEnd;
shared_ptr<data_entry> * dataCurrent;
process_state state;
int Deadline;
int totalTime;
};
struct event_entry {
shared_ptr<process_entry> * process;
int time;
event_component component;
};
struct device_item {
shared_ptr<process_entry> * process;
int finishTime;
int startTime;
int endTime;
};
class device_table {
device_item * core1;
device_item * core2;
device_item * core3;
device_item * core4;
device_item * disk1;
std::queue<device_item*> IQ;
std::queue<device_item*> RQ;
std::queue<device_item*> DiskQ;
int completedReal;
int completedInter;
int completedDisk;
double DurationDisk;
int CoreUtilization[4];
int DiskUtilization;
public:
device_table()
{
core1 = NULL;
core2 = NULL;
core3 = NULL;
core4 = NULL;
disk1 = NULL;
completedReal = 0;
completedInter = 0;
completedDisk = 0;
DurationDisk = 0;
DiskUtilization = 0;
CoreUtilization[0] = 0;
CoreUtilization[1] = 0;
CoreUtilization[2] = 0;
CoreUtilization[3] = 0;
}
void addCore(shared_ptr<process_entry> * process, int currentTime)
{
if((core1 != NULL && core2 != NULL && core3 != NULL && core4 != NULL))
{
string processType = process->get()->dataStart->get()->text;
device_item * newEntry = new device_item();
newEntry->process = process;
newEntry->finishTime = currentTime + process->get()->dataCurrent->get()->time;
newEntry->startTime = currentTime;
if(core1->process->get()->dataStart->get()->text == "INTERACTIVE" && processType == "REAL-TIME")
{
core1->finishTime = core1->finishTime - currentTime;
IQ.push(core1);
core1 = newEntry;
}
else if(core2->process->get()->dataStart->get()->text == "INTERACTIVE" && processType == "REAL-TIME")
{
core2->finishTime = core2->finishTime - currentTime;
IQ.push(core2);
core2 = newEntry;
}
else if(core3->process->get()->dataStart->get()->text == "INTERACTIVE" && processType == "REAL-TIME")
{
core3->finishTime = core3->finishTime - currentTime;
IQ.push(core3);
core3 = newEntry;
}
else if(core4->process->get()->dataStart->get()->text == "INTERACTIVE" && processType == "REAL-TIME")
{
core4->finishTime = core4->finishTime - currentTime;
IQ.push(core4);
core4 = newEntry;
}
else
{
newEntry->finishTime = 0;
newEntry->startTime = 0;
if(process->get()->dataCurrent->get()->text == "INTERACTIVE")
{
IQ.push(newEntry);
process->get()->state = process_state::WAITING;
}
else
{
newEntry->finishTime = process->get()->dataCurrent->get()->time;
RQ.push(newEntry);
process->get()->state = process_state::WAITING;
}
}
}
else
{
device_item * newEntry = new device_item();
newEntry->process = process;
newEntry->startTime = currentTime;
newEntry->finishTime = currentTime + process->get()->dataCurrent->get()->time;
process->get()->state = process_state::RUNNING;
if(core1 == NULL)
core1 = newEntry;
else if(core2 == NULL)
core2 = newEntry;
else if(core3 == NULL)
core3 = newEntry;
else if(core4 == NULL)
core4 = newEntry;
}
}
void addDisk(shared_ptr<process_entry> * disk, int currentTime)
{
device_item * newDisk = new device_item();
newDisk->process = disk;
newDisk->finishTime = currentTime + disk->get()->dataCurrent->get()->time;
newDisk->startTime = currentTime;
if(disk1 == NULL)
disk1 = newDisk;
else
{
newDisk->finishTime = 0;
newDisk->startTime = 0;
DiskQ.push(newDisk);
disk->get()->state = process_state::WAITING;
}
}
void checkCoreAndDisk(int currentTime)
{
// Stick queue if becomes empty. Subtract time.
if(disk1 != NULL)
{
if(disk1->finishTime <= currentTime) {
DiskUtilization += disk1->finishTime - disk1->startTime;
disk1->process->get()->state = process_state::READY;
DurationDisk += disk1->finishTime - disk1->startTime;
disk1 = NULL;
completedDisk += 1;
if(DiskQ.size() > 0) {
disk1 = DiskQ.front();
disk1->startTime = currentTime;
disk1->finishTime = currentTime + disk1->process->get()->dataCurrent->get()->time;
DiskQ.pop();
disk1->process->get()->state = process_state::RUNNING;
}
}
}
if(core1 != NULL)
{
if(core1->finishTime <= currentTime) {
CoreUtilization[0] += core1->finishTime - core1->startTime;
core1->process->get()->state = process_state::READY;
if(core1->process->get()->dataStart->get()->text == "INTERACTIVE") { completedInter += 1; } else { completedReal += 1; };
core1 = NULL;
if(RQ.size() > 0) {
core1 = RQ.front();
core1->startTime = currentTime;
core1->finishTime = currentTime + core1->finishTime;
RQ.pop();
core1->process->get()->state = process_state::RUNNING;
}
else if(IQ.size() > 0) {
core1 = IQ.front();
core1->startTime = currentTime;
core1->finishTime = currentTime + core1->finishTime;
IQ.pop();
core1->process->get()->state = process_state::RUNNING;
}
}
}
if(core2 != NULL)
{
if(core2->finishTime <= currentTime) {
CoreUtilization[1] += core2->finishTime - core2->startTime;
core2->process->get()->state = process_state::READY;
if(core2->process->get()->dataStart->get()->text == "INTERACTIVE") { completedInter += 1; } else { completedReal += 1; };
core2 = NULL;
if(RQ.size() > 0) {
core2 = RQ.front();
core2->startTime = currentTime;
core2->finishTime = currentTime + core2->finishTime;
RQ.pop();
core2->process->get()->state = process_state::RUNNING;
}
else if(IQ.size() > 0) {
core2 = IQ.front();
core2->startTime = currentTime;
core2->finishTime = currentTime + core2->finishTime;
IQ.pop();
core2->process->get()->state = process_state::RUNNING;
}
}
}
if(core3 != NULL)
{
if(core3->finishTime <= currentTime) {
CoreUtilization[2] += core3->finishTime - core3->startTime;
core3->process->get()->state = process_state::READY;
if(core3->process->get()->dataStart->get()->text == "INTERACTIVE") { completedInter += 1; } else { completedReal += 1; };
core3 = NULL;
if(RQ.size() > 0) {
core3 = RQ.front();
core3->startTime = currentTime;
core3->finishTime = currentTime + core3->finishTime;
RQ.pop();
core3->process->get()->state = process_state::RUNNING;
}
else if(IQ.size() > 0) {
core3 = IQ.front();
core3->startTime = currentTime;
core3->finishTime = currentTime + core3->finishTime;
IQ.pop();
core3->process->get()->state = process_state::RUNNING;
}
}
}
if(core4 != NULL)
{
if(core4->finishTime <= currentTime) {
CoreUtilization[3] += core4->finishTime - core4->startTime;
core4->process->get()->state = process_state::READY;
if(core4->process->get()->dataStart->get()->text == "INTERACTIVE") { completedInter += 1; } else { completedReal += 1; };
core4 = NULL;
if(RQ.size() > 0) {
core4 = RQ.front();
core4->startTime = currentTime;
core4->finishTime = currentTime + core4->finishTime;
RQ.pop();
core4->process->get()->state = process_state::RUNNING;
}
else if(IQ.size() > 0) {
core4 = IQ.front();
core4->startTime = currentTime;
core4->finishTime = currentTime + core4->finishTime;
IQ.pop();
core4->process->get()->state = process_state::RUNNING;
}
}
}
}
bool checkDeadlines()
{
}
bool checkCoreFull()
{
return (core1 != NULL && core2 != NULL && core3 != NULL && core4 != NULL);
}
int printCompletedInt()
{
return completedInter;
}
int printCompletedReal()
{
return completedReal;
}
int printCompletedDisk()
{
return completedDisk;
}
double printAvgDiskDuration()
{
return (DurationDisk / completedDisk);
}
int printCoreUtilization(int core)
{
return CoreUtilization[core - 1];
}
int printDiskUtilization()
{
return DiskUtilization;
}
};
void createDataTable(string file, vector<shared_ptr<data_entry>> &dataTable)
{
string line;
ifstream myfile (file);
if (myfile.is_open())
{
int processAmount = 0;
while ( getline (myfile,line) )
{
if(line.find("INTERACTIVE") != string::npos)
{
shared_ptr<data_entry> newEntry = make_shared<data_entry>();
newEntry->text = "INTERACTIVE";
newEntry->time = atoi(line.substr(12, 12).c_str());
dataTable.push_back(newEntry);
//shared_ptr<process_entry> newProcessEntry = shared_ptr<process_entry>(new process_entry);
//newProcessEntry->dataStart = &dataTable.at(dataTable.size() - 1);
//newProcessEntry->dataCurrent = &dataTable.at(dataTable.size() - 1);
//newProcessEntry->process = processAmount;
//processTable.push_back(newProcessEntry);
processAmount += 1;
}
else if (line.find("REAL-TIME") != string::npos)
{
shared_ptr<data_entry> newEntry = make_shared<data_entry>();
newEntry->text = "REAL-TIME";
newEntry->time = atoi(line.substr(10, 10).c_str());
dataTable.push_back(newEntry);
processAmount += 1;
}
else if(line.find("END") != string::npos)
{
shared_ptr<data_entry> newEntry = make_shared<data_entry>();
newEntry->text = "END";
newEntry->time = -1;
dataTable.push_back(newEntry);
//processTable.at(processTable.size())->dataEnd = &dataTable.at(dataTable.size());
}
else if(line.find("RUN") != string::npos)
{
shared_ptr<data_entry> newEntry = make_shared<data_entry>();
newEntry->text = "RUN";
newEntry->time = atoi(line.substr(4, 4).c_str());
dataTable.push_back(newEntry);
}
else if(line.find("TTY") != string::npos)
{
shared_ptr<data_entry> newEntry = make_shared<data_entry>();
newEntry->text = "TTY";
newEntry->time = atoi(line.substr(4, 4).c_str());
dataTable.push_back(newEntry);
}
else if(line.find("DISK") != string::npos)
{
shared_ptr<data_entry> newEntry = make_shared<data_entry>();
newEntry->text = "DISK";
newEntry->time = atoi(line.substr(5, 5).c_str());
dataTable.push_back(newEntry);
}
else if(line.find("DEADLINE") != string::npos)
{
shared_ptr<data_entry> newEntry = make_shared<data_entry>();
newEntry->text = "DEADLINE";
newEntry->time = atoi(line.substr(9, 9).c_str());
dataTable.push_back(newEntry);
}
}
}
myfile.close();
}
void createProcessTable(vector<shared_ptr<data_entry>> &dataTable, vector<shared_ptr<process_entry>> &processTable)
{
int processAmount = 0;
for(size_t i = 0; i < dataTable.size(); i++)
{
if(dataTable.at(i)->text == "INTERACTIVE" || dataTable.at(i)->text == "REAL-TIME")
{
shared_ptr<process_entry> newProcessEntry = make_shared<process_entry>();
newProcessEntry->dataStart = &dataTable.at(i);
newProcessEntry->dataCurrent = &dataTable.at(i);
newProcessEntry->process = processAmount;
newProcessEntry->state = process_state::P_NONE;
newProcessEntry->totalTime = 0;
if(dataTable.at(i + 1)->text == "DEADLINE")
newProcessEntry->Deadline = dataTable.at(i + 1)->time;
else
newProcessEntry->Deadline = -1;
processTable.push_back(newProcessEntry);
processAmount += 1;
}
else if(dataTable.at(i)->text == "END")
{
processTable.back()->dataEnd = &dataTable.at(i);
}
if(processTable.size() > 0 && dataTable.at(i)->text != "DEADLINE" && dataTable.at(i)->time != -1)
{
processTable.back()->totalTime += dataTable.at(i)->time;
}
}
}
void createEventTable(vector<shared_ptr<process_entry>> &processTable, vector<shared_ptr<event_entry>> &eventTable)
{
for(size_t i = 0; i < processTable.size(); i++)
{
shared_ptr<event_entry> newEventEntry = make_shared<event_entry>();
newEventEntry->process = &processTable.at(i);
newEventEntry->time = newEventEntry->process->get()->dataStart->get()->time;
newEventEntry->component = event_component::E_NONE;
eventTable.push_back(newEventEntry);
}
}
bool sortEventTable(shared_ptr<event_entry> i, shared_ptr<event_entry> j)
{
return (i->time < j->time);
}
int main(int argc, const char *argv[])
{
int currentTime = 0;
int deadlineAmount = 0;
vector<shared_ptr<data_entry>> dataTable;
vector<shared_ptr<process_entry>> processTable;
vector<shared_ptr<event_entry>> eventTable;
device_table newDevice;
createDataTable(argv[1], dataTable);
createProcessTable(dataTable, processTable);
createEventTable(processTable, eventTable);
std::sort (eventTable.begin(), eventTable.end(), sortEventTable);
while(eventTable.size() > 0)
{
std::sort (eventTable.begin(), eventTable.end(), sortEventTable);
newDevice.checkCoreAndDisk(currentTime);
//newDevice.printAll();
string currentText = eventTable.at(0).get()->process->get()->dataCurrent->get()->text;
if(currentText == "RUN")
{
eventTable.at(0).get()->component = event_component::CORE;
newDevice.addCore(eventTable.at(0).get()->process, currentTime);
}
else if(currentText == "REAL-TIME" || currentText == "INTERACTIVE")
{
eventTable.at(0).get()->component = event_component::START;
}
else if(currentText == "DISK")
{
eventTable.at(0).get()->component = event_component::DISK;
newDevice.addDisk(eventTable.at(0).get()->process, currentTime);
}
else if(currentText == "TTY")
{
eventTable.at(0).get()->component = event_component::TTY;
}
if(eventTable.at(0).get()->process->get()->dataCurrent->get()->text == "END")
{
//cout << eventTable.at(0).get()->time << "\n";
if(eventTable.at(0).get()->process->get()->Deadline == 1)
{
}
eventTable.erase(eventTable.begin());
}
else
{
//cout << eventTable.at(0).get()->time << "\n";
eventTable.at(0).get()->process->get()->dataCurrent += 1;
if(currentText != "DEADLINE")
currentTime = eventTable.at(0).get()->time;
if(eventTable.at(0).get()->process->get()->dataCurrent->get()->time != -1 && currentText != "DEADLINE")
eventTable.at(0).get()->time = currentTime + eventTable.at(0).get()->process->get()->dataCurrent->get()->time;
}
//cout << newDevice.checkCoreFull() << "\n";
}
for(size_t i = 0; i < processTable.size(); i++)
{
if(processTable.at(i)->Deadline != -1)
{
if (processTable.at(i)->totalTime > processTable.at(i)->Deadline)
deadlineAmount += 1;
}
}
cout << "Completed Interactive : " << newDevice.printCompletedInt() << "\n";
cout << "Completed Real-Time : " << newDevice.printCompletedReal() << "\n";
cout << "Completed Disk : " << newDevice.printCompletedDisk() << "\n";
cout << "Core1 Utilization : " << newDevice.printCoreUtilization(1) << "\n";
cout << "Core2 Utilization : " << newDevice.printCoreUtilization(2) << "\n";
cout << "Core3 Utilization : " << newDevice.printCoreUtilization(3) << "\n";
cout << "Core4 Utilization : " << newDevice.printCoreUtilization(4) << "\n";
cout << "Disk Utilization : " << newDevice.printDiskUtilization() << "\n";
cout << "Average Disk Duration : " << newDevice.printAvgDiskDuration() << "\n";
cout << "% Missed Deadline : " << ((float)deadlineAmount / processTable.size()) * 100 << "%\n";
cout << "Total Elapsed Time : " << currentTime << "\n";
int input;
cin >> input;
return 0;
}

You may want to compile your code with C++11 support:
g++ -std=c++11

Related

Issue in reverse polish notation task in C++

The program analyzes the entered expression, converts it into reverse Polish notation and calculates the result.My program works correctly with expressions without brackets, but if I use an expression with brackets, I get an error: "Op" was nullptr. I have no idea why as my Stack "Op" must be initialized.(I marked where the error occurs in the code).
My code:
#include <iostream>
#include <string>
#include <iomanip>
struct Stack {
char info;
Stack* next;
} *begin;
int Prior(char);
Stack* InStack(Stack*, char);
Stack* OutStack(Stack*, char&);
double Result(char*);
double mas[201];
Stack* InStack(Stack* p, char in) {
Stack* t = new Stack;
t->info = in;
t->next = p;
return t;
}
Stack* OutStack(Stack* p, char& in) {
Stack* t = p;
in = p->info;
p = p->next;
delete t;
return p;
}
int Prior(char a) {
switch (a) {
case '^': return 4;
case '*': case '/': return 3;
case '-': case '+': return 2;
case '(': return 1;
}
return 0;
}
double Result(char* str) {
int i;
char ss, ss1, ss2, ssR = 'z' + 1;
double op1, op2, res = 0, mas[200];
std::cout << "Input data:" << std::endl;
for (i = 0; str[i] != '\0'; ++i)
{
ss = str[i];
if (ss >= 'a' && ss <= 'z')
{
std::cout << ss << " = ";
std::cin >> mas[int(ss)];
}
}
for (i = 0; str[i] != '\0'; ++i)
{
ss = str[i];
if (!(ss == '+' || ss == '-' || ss == '*' || ss == '/'))
{
begin = InStack(begin, ss);
}
else
{
begin = OutStack(begin, ss2);
begin = OutStack(begin, ss1);
op2 = mas[int(ss2)];
op1 = mas[int(ss1)];
switch (ss)
{
case'+':res = op1 + op2; break;
case'-':res = op1 - op2; break;
case'*':res = op1 * op2; break;
case'/':res = op1 / op2; break;
}
mas[int(ssR)] = res;
begin = InStack(begin, ssR);
ssR++;
}
}
return res;
}
int main()
{
Stack* t, * Op = NULL;
char a;
char In[81], Out[81];
int k = 0, l = 0;
std::cout << "Input form: " << std::endl;
std::cin >> In;
while (In[k] != '\0')
{
if (In[k] >= 'a' && In[k] <= 'z')
{
Out[l++] = In[k];
}
if (In[k] == '(')
{
Op == InStack(Op, In[k]);
}
if (In[k] == ')')
{
while ((Op->info) != '(') //ERROR HERE
{
Op = OutStack(Op, a);
if (!Op)
{
a = '\0';
}
Out[l++] = a;
}
t = Op;
Op = Op->next;
delete t;
}
if (In[k] == '+' || In[k] == '-' || In[k] == '*' || In[k] == '/')
{
while (Op != NULL && Prior(Op->info) >= Prior(In[k]))
{
Op = OutStack(Op, a);
Out[l++] = a;
}
Op = InStack(Op, In[k]);
}
k++;
}
while (Op != NULL)
{
Op = OutStack(Op, a);
Out[l++] = a;
}
Out[l] = '\0';
std::cout << "\nPolish: " << Out << std::endl;
std::cout << "\nRes = " << Result(Out) << std::endl;
}

How to fix "error:expected primary-expression before ‘,’ token"

I am trying to make a first fit memory management code but everytime I try to run it I keep getting incomplete results in the output and these errors
error:expected primary-expression before ‘,’ token
I don't know what to put in the code to resolve this error
#include"stdafx.h"
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<memory.h>
#include <cstdlib>
using namespace std;
struct allocList
{
char* startAlloc;
char* endAlloc;
int fk_pid;
allocList *nxt;
};
struct procList
{
int pid;
int jobstatus;
char *startProc;
char *endProc;
procList *nxt;
};
bool initProc(procList*&, int*, int);
bool initAlloc(allocList*&, int*, int);
int doFirstFit(procList*, allocList*);
int cmptFragmentation(procList*, allocList*);
int search(procList*, int);
bool reset(procList*, allocList*);
int main()
{
int arrMemory[] = { 100, 500, 200, 300, 600 };
int arrJobs[] = { 212, 17, 112, 426, 500 };
allocList *ptrAllocStart = NULL;
procList *ptrProcStart = NULL;
initAlloc(ptrAllocStart, arrMemory, (sizeof(arrMemory) / sizeof(int)));
initProc(ptrProcStart, arrJobs, (sizeof(arrJobs) / sizeof(int)));
cout << "Memory Block: " << endl << "Block\tSpace" << endl;
for (int i = 0; i < sizeof(arrMemory) / sizeof(int); i++)
{
cout << i + 1 << "\t" << arrMemory[i] << endl;
}
cout << "\nJobs:" << endl << "Job\tSize" << endl;
for (int i = 0; i < sizeof(arrJobs) / sizeof(int); i++)
{
cout << i + 1 << "\t" << arrJobs[i] << endl;
}
int jobLoaded = 0;
int fragmentation = 0;
jobLoaded = doFirstFit(ptrProcStart, ptrAllocStart);
fragmentation = cmptFragmentation(ptrProcStart, ptrAllocStart);
allocList* memtrav = ptrAllocStart;
getch();
return 0;
}
bool initProc(procList*& ptrProcStart, int* ptrArrProc, int length){
int i;
procList *ptrProc=ptrProcStart;
for(i=0; i<length; i++){
if(ptrProc != NULL){
ptrProc->nxt=new procList;
ptrProc = ptrProc->nxt; ptrProc->startProc=(char*)malloc(*(ptrArrProc+i));
ptrProc->endProc=ptrProc->startProc + *(ptrArrProc+i);
memset(ptrProc->startProc,'a'+i,*(ptrArrProc+i));
ptrProc->jobstatus=0;
ptrProc->pid=i;
ptrProc->nxt=NULL;
}
else
{
ptrProc=new procList;
ptrProc->startProc=(char*)malloc(*(ptrArrProc+i));
ptrProc->endProc=ptrProc->startProc + *(ptrArrProc+i);
memset(ptrProc->startProc, 'a'+i, *(ptrArrProc+i));
ptrProc->jobstatus=0;
ptrProc->pid=i;
ptrProc->nxt=NULL;
ptrProcStart = ptrProc;}}
return true; }
bool initAlloc(allocList*& ptrAllocstart, int* ptrArrAlloc, int length)
{
//cout << "loading function initAlloc"<< "\t" << length << endl;
int i;
allocList* ptrAlloc = ptrAllocstart;
for (i = 0; i < length; i++)
{
//cout << "running loop 1st"<< "\t" << i << endl;
if (ptrAlloc != NULL)
{
ptrAlloc -> nxt = new allocList;
ptrAlloc = ptrAlloc->nxt;
//cout << "after new ptrAlloc" << endl;
ptrAlloc->startAlloc=(char*)malloc(*(ptrArrAlloc+i));
ptrAlloc->endAlloc=ptrAlloc->startAlloc + *(ptrArrAlloc+i);
memset(ptrAlloc->startAlloc,'a'+i,*(ptrArrAlloc+i));
ptrAlloc->nxt=NULL;
}
else
{ //cout << "inside else"<< "\t" << i << endl;
ptrAlloc= new allocList;
ptrAlloc->startAlloc-(char*)malloc(*(ptrArrAlloc+i));
ptrAlloc->endAlloc-ptrAlloc->startAlloc + *(ptrArrAlloc+i);
memset(ptrAlloc->startAlloc, 'a'+i, *(ptrArrAlloc+i));
ptrAlloc->nxt=NULL;
ptrAllocstart=ptrAlloc;
}
}
return true;
}
int doFirstFit(procList*, allocList*){
//cout lang ng UI
cout << "\n\nFirst Fit:\nMemory Block\tSize\tJob\tInternal " << " Fragmentation\n" << endl;
//declaration ng variable
int i = 0;
allocList* memory;
//mag do while sa memory n walang laman?
while (memory != NULL)
i++;
cout << "\t" << i << "\t" << memory->endAlloc - memory->startAlloc << "\t"
<< memory->fk_pid << "\t"
//<< memory->endAlloc - memory->startAlloc - search(procList,memory->fk_pid - 1)
<< endl;
memory = memory->nxt;
return 0;
}
int search(procList* job, int id)
{
int size = 0;
while (job != NULL)
{
if (job->pid == id)
{
size =atoi(job->endProc) - atoi(job->startProc);
break;
}
job = job->nxt;
}
return size;
}
int cmptFragmentation(procList * jobs, allocList * mem)
{
allocList* memtrav, * temp;
procList* jobtrav;
jobtrav = jobs;
memtrav = mem;
int freespace = 0, memsize, jobsize;
int i = 0;
while (memtrav->nxt != NULL)
{
if (memtrav->nxt->fk_pid == 0)
{
freespace += (memtrav->nxt->endAlloc - memtrav->nxt->startAlloc);
temp = memtrav->nxt;
memtrav->nxt = memtrav->nxt->nxt; delete temp;
}
memtrav = memtrav->nxt;
}
if (memtrav->fk_pid == 0)
{
freespace += (memtrav->endAlloc - memtrav->startAlloc);
memtrav = memtrav->nxt;
}
memtrav = mem;
while (memtrav != NULL)
{
jobsize = search(jobs, memtrav->fk_pid - 1);
memsize = memtrav->endAlloc - memtrav->startAlloc;
if (memtrav->fk_pid != 0)
{
memtrav->endAlloc = memtrav->startAlloc + jobsize;
}
freespace += (memsize - jobsize);
memtrav = memtrav->nxt;
}
memtrav = mem;
while (memtrav != NULL)
{
if (memtrav->nxt == NULL)
{
memtrav->nxt = new allocList;
memtrav = memtrav->nxt;
memtrav->startAlloc = (char*)malloc(freespace);
memtrav->endAlloc = memtrav->startAlloc + freespace;
memset(memtrav->startAlloc, 0, freespace);
memtrav->fk_pid = 0;
memtrav->nxt = NULL;
break;
}
memtrav = memtrav->nxt;
}
memtrav = mem;
cout << endl << endl << "Defragmentation\nMemory " << "Block\tSize\tJob\tFreeSpace\n";
while (memtrav != NULL)
{
i++;
cout << "\t" << i << "\t" << memtrav->endAlloc - memtrav->startAlloc << "\t" << memtrav->fk_pid
<< "\t" << memtrav->endAlloc - memtrav->startAlloc - search(jobs, memtrav->fk_pid - 1) << endl;
memtrav = memtrav->nxt;
}
while (jobtrav != NULL)
{
if (jobtrav->jobstatus == 0)
{
doFirstFit(jobs, mem);
cmptFragmentation(jobs, mem);
}
jobtrav = jobtrav->nxt;
}
return 0;
}
bool reset(procList* jobs, allocList* mem)
{
procList* tempj = jobs;
allocList* tempa = mem;
while (jobs->nxt != NULL)
{
jobs = jobs->nxt;
free(tempj);
tempj = jobs;
}
free(tempj);
while (mem->nxt != NULL)
{
mem = mem->nxt;
free(tempa);
tempa = mem;
}
free(tempa);
return true;
}
This is the part where I commented where the error has an issue with and I don't know what I'm doing wrong
<< memory->endAlloc - memory->startAlloc - search(procList,memory->fk_pid - 1)
Please help, thank you so much!
In this function call
search(procList,memory->fk_pid - 1)
there is used the type specifier procList instead of an expression.
Pay attention to that such a function definition where its parameters are not used
int doFirstFit(procList*, allocList*){
//cout lang ng UI
cout << "\n\nFirst Fit:\nMemory Block\tSize\tJob\tInternal " << " Fragmentation\n" << endl;
//declaration ng variable
int i = 0;
allocList* memory;
//..
does not make a sense.
Moreover there is used uninitialized pointer
allocList* memory;
in expressions like for example this
memory = memory->nxt;
that invokes undefined behavior.
The function is called with arguments as
doFirstFit(jobs, mem);
So you need use the passed arguments within the function instead of declaring local variables like this
allocList* memory;
In the analysis I've done the error is this
memory->endAlloc - memory->startAlloc - search(procList,memory->fk_pid - 1)
otherwise direct passing of proclist, you must declare its pointer variable just like you did for Alloclist memory; by same way you'll resolve this
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<memory.h>
#include <cstdlib>
using namespace std;
struct allocList
{
char* startAlloc;
char* endAlloc;
int fk_pid;
allocList *nxt;
};
struct procList
{
int pid;
int jobstatus;
char *startProc;
char *endProc;
procList *nxt;
};
bool initProc(procList*&, int*, int);
bool initAlloc(allocList*&, int*, int);
int doFirstFit(procList*, allocList*);
int cmptFragmentation(procList*, allocList*);
int search(procList*, int);
bool reset(procList*, allocList*);
int main()
{
int arrMemory[] = { 100, 500, 200, 300, 600 };
int arrJobs[] = { 212, 17, 112, 426, 500 };
allocList *ptrAllocStart = NULL;
procList *ptrProcStart = NULL;
initAlloc(ptrAllocStart, arrMemory, (sizeof(arrMemory) / sizeof(int)));
initProc(ptrProcStart, arrJobs, (sizeof(arrJobs) / sizeof(int)));
cout << "Memory Block: " << endl << "Block\tSpace" << endl;
for (int i = 0; i < sizeof(arrMemory) / sizeof(int); i++)
{
cout << i + 1 << "\t" << arrMemory[i] << endl;
}
cout << "\nJobs:" << endl << "Job\tSize" << endl;
for (int i = 0; i < sizeof(arrJobs) / sizeof(int); i++)
{
cout << i + 1 << "\t" << arrJobs[i] << endl;
}
int jobLoaded = 0;
int fragmentation = 0;
jobLoaded = doFirstFit(ptrProcStart, ptrAllocStart);
fragmentation = cmptFragmentation(ptrProcStart, ptrAllocStart);
allocList* memtrav = ptrAllocStart;
getch();
return 0;
}
bool initProc(procList*& ptrProcStart, int* ptrArrProc, int length){
int i;
procList *ptrProc=ptrProcStart;
for(i=0; i<length; i++){
if(ptrProc != NULL){
ptrProc->nxt=new procList;
ptrProc = ptrProc->nxt; ptrProc->startProc=(char*)malloc(*(ptrArrProc+i));
ptrProc->endProc=ptrProc->startProc + *(ptrArrProc+i);
memset(ptrProc->startProc,'a'+i,*(ptrArrProc+i));
ptrProc->jobstatus=0;
ptrProc->pid=i;
ptrProc->nxt=NULL;
}
else
{
ptrProc=new procList;
ptrProc->startProc=(char*)malloc(*(ptrArrProc+i));
ptrProc->endProc=ptrProc->startProc + *(ptrArrProc+i);
memset(ptrProc->startProc, 'a'+i, *(ptrArrProc+i));
ptrProc->jobstatus=0;
ptrProc->pid=i;
ptrProc->nxt=NULL;
ptrProcStart = ptrProc;}}
return true; }
bool initAlloc(allocList*& ptrAllocstart, int* ptrArrAlloc, int length)
{
//cout << "loading function initAlloc"<< "\t" << length << endl;
int i;
allocList* ptrAlloc = ptrAllocstart;
for (i = 0; i < length; i++)
{
//cout << "running loop 1st"<< "\t" << i << endl;
if (ptrAlloc != NULL)
{
ptrAlloc -> nxt = new allocList;
ptrAlloc = ptrAlloc->nxt;
//cout << "after new ptrAlloc" << endl;
ptrAlloc->startAlloc=(char*)malloc(*(ptrArrAlloc+i));
ptrAlloc->endAlloc=ptrAlloc->startAlloc + *(ptrArrAlloc+i);
memset(ptrAlloc->startAlloc,'a'+i,*(ptrArrAlloc+i));
ptrAlloc->nxt=NULL;
}
else
{ //cout << "inside else"<< "\t" << i << endl;
ptrAlloc= new allocList;
ptrAlloc->startAlloc-(char*)malloc(*(ptrArrAlloc+i));
ptrAlloc->endAlloc-ptrAlloc->startAlloc + *(ptrArrAlloc+i);
memset(ptrAlloc->startAlloc, 'a'+i, *(ptrArrAlloc+i));
ptrAlloc->nxt=NULL;
ptrAllocstart=ptrAlloc;
}
}
return true;
}
int doFirstFit(procList*, allocList*){
//cout lang ng UI
cout << "\n\nFirst Fit:\nMemory Block\tSize\tJob\tInternal " << " Fragmentation\n" << endl;
//declaration ng variable
int i = 0;
allocList* memory;
procList*ab;
//mag do while sa memory n walang laman?
while (memory != NULL)
i++;
int mem=memory->fk_pid - 1;
cout << "\t" << i << "\t" << memory->endAlloc - memory->startAlloc << "\t"<< memory->fk_pid << "\t"<< memory->endAlloc - memory->startAlloc - search(ab,mem)<< endl;
memory = memory->nxt;
return 0;
}
int search(procList* job, int id)
{
int size = 0;
while (job != NULL)
{
if (job->pid == id)
{
size =atoi(job->endProc) - atoi(job->startProc);
break;
}
job = job->nxt;
}
return size;
}
int cmptFragmentation(procList * jobs, allocList * mem)
{
allocList* memtrav, * temp;
procList* jobtrav;
jobtrav = jobs;
memtrav = mem;
int freespace = 0, memsize, jobsize;
int i = 0;
while (memtrav->nxt != NULL)
{
if (memtrav->nxt->fk_pid == 0)
{
freespace += (memtrav->nxt->endAlloc - memtrav->nxt->startAlloc);
temp = memtrav->nxt;
memtrav->nxt = memtrav->nxt->nxt; delete temp;
}
memtrav = memtrav->nxt;
}
if (memtrav->fk_pid == 0)
{
freespace += (memtrav->endAlloc - memtrav->startAlloc);
memtrav = memtrav->nxt;
}
memtrav = mem;
while (memtrav != NULL)
{
jobsize = search(jobs, memtrav->fk_pid - 1);
memsize = memtrav->endAlloc - memtrav->startAlloc;
if (memtrav->fk_pid != 0)
{
memtrav->endAlloc = memtrav->startAlloc + jobsize;
}
freespace += (memsize - jobsize);
memtrav = memtrav->nxt;
}
memtrav = mem;
while (memtrav != NULL)
{
if (memtrav->nxt == NULL)
{
memtrav->nxt = new allocList;
memtrav = memtrav->nxt;
memtrav->startAlloc = (char*)malloc(freespace);
memtrav->endAlloc = memtrav->startAlloc + freespace;
memset(memtrav->startAlloc, 0, freespace);
memtrav->fk_pid = 0;
memtrav->nxt = NULL;
break;
}
memtrav = memtrav->nxt;
}
memtrav = mem;
cout << endl << endl << "Defragmentation\nMemory " << "Block\tSize\tJob\tFreeSpace\n";
while (memtrav != NULL)
{
i++;
cout << "\t" << i << "\t" << memtrav->endAlloc - memtrav->startAlloc << "\t" << memtrav->fk_pid
<< "\t" << memtrav->endAlloc - memtrav->startAlloc - search(jobs, memtrav->fk_pid - 1) << endl;
memtrav = memtrav->nxt;
}
while (jobtrav != NULL)
{
if (jobtrav->jobstatus == 0)
{
doFirstFit(jobs, mem);
cmptFragmentation(jobs, mem);
}
jobtrav = jobtrav->nxt;
}
return 0;
}
bool reset(procList* jobs, allocList* mem)
{
procList* tempj = jobs;
allocList* tempa = mem;
while (jobs->nxt != NULL)
{
jobs = jobs->nxt;
free(tempj);
tempj = jobs;
}
free(tempj);
while (mem->nxt != NULL)
{
mem = mem->nxt;
free(tempa);
tempa = mem;
}
free(tempa);
return true;
}
The above working just fine.

UndefinedBehaviorSanitizer: SEGV on unknown address

Hello, i was solving this -> kata on CodeWars.
I am using a VusialStudio 2019 to write code. When i start this project in VS no erros is presented to me. The program even works correctly for the first test case. But when i am testing it on problem's page it gives me an error:
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x000000429d24 bp 0x7ffc2f3f72b0 sp 0x7ffc2f3f7150 T1)
==1==The signal is caused by a READ memory access.
==1==Hint: address points to the zero page.
==1==WARNING: invalid path to external symbolizer!
==1==WARNING: Failed to use and restart external symbolizer!
#0 0x429d23 (/workspace/test+0x429d23)
#1 0x42ab1d (/workspace/test+0x42ab1d)
#2 0x429fa3 (/workspace/test+0x429fa3)
#3 0x42e2f2 (/workspace/test+0x42e2f2)
#4 0x42c9ce (/workspace/test+0x42c9ce)
#5 0x42c529 (/workspace/test+0x42c529)
#6 0x42c11b (/workspace/test+0x42c11b)
#7 0x431425 (/workspace/test+0x431425)
#8 0x42a08d (/workspace/test+0x42a08d)
#9 0x7f1b94116bf6 (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)
#10 0x4053e9 (/workspace/test+0x4053e9)
UndefinedBehaviorSanitizer can not provide additional info.
==1==ABORTING
For a while i was turning on and off parts of program to determen where the problem is. I nereved it down to this line
Program::Status_enum Program::ExecuteComand(Comand_Class* ComandToExe)
{
else if (ComandToExe->name == "div")
{
this->Registers[ComandToExe->args[0]] /= GetValue(ComandToExe->args[1]); // error here
}
}
Leter i found out that error was showing up even if i chage line to
this->Registers.begin()->second = 3; // same error
this->Registers["a"] = 2; // same error
It looks to me that any change of value causes it to throw an error. But reaading for example was ok
int a = Registers["a"];
std::cout << a; // will print a right value
So this basikly all i was able to find out by my self. Here is structer of my code.
Basikly my goal is to write a Asmbler intorpritator
// All Comands saved as this class
class Comand_Class
{
public:
std::string name; // for example mov, div, etc.
std::vector<std::string> args; // data for the comand
Comand_Class(std::string Name, std::string arg1 = "", std::string arg2 = "")
{
//..
}
Comand_Class(std::string Name, std::vector<std::string> Args)
{
//..
}
};
// I use this class to store functions cod as a small program itself
class Function_Class
{
public:
std::vector<Comand_Class*> FunctionProgram;
};
// the main class
class Program
{
std::string MSG = "-1"; // for outout
std::unordered_map <std::string, int> Registers; // map in which the problem exsist
std::vector<Comand_Class*> ProgramLines; // Starting code
std::map<std::string,Function_Class*> CallList; // Name of Function and Her code to exe
// .exe is giving me back the status, after executing a Comand_Class line, this how i know what to do next.
enum Status_enum
{
END = 0,
CALL = 1,
JUMP = 2,
RET = 3,
CONTINUE = 10,
ERROR = -1,
};
public:
std::string Start()
{
exe(this -> ProgramLines );
return MSG;
}
int GetValue(std::string arg)
{
if ('a' <= arg[0] && arg[0] <= 'z')
{
//register
return this->Registers[arg];
}
else
{
int a = std::stoi(arg);
return a;
}
}
// not related to the problem,
void preprocesing(std::string program);
Status_enum ExecuteComand(Comand_Class* ComandToExe);
void exe(std::vector<Comand_Class*> ProgramLinesToExe);
~Program()
{
for (unsigned long i = 0; i < ProgramLines.size(); i++)
{
Comand_Class* del = ProgramLines.at(i);
delete del;
}
for (auto it = CallList.begin(); it != CallList.end(); it++)
{
for (unsigned long i = 0; i < it->second->FunctionProgram.size(); i++)
{
Comand_Class* del = it->second->FunctionProgram.at(i);
delete del;
}
}
// add CallList to clear
}
};
// this function is recurseve, Can this be a reson why it brakes?
void Program::exe(std::vector<Comand_Class*> ProgramLinesToExe)
{
Status_enum Status;
unsigned long int SubPC = 0; // Comand to exe
while (SubPC < ProgramLinesToExe.size())
{
Status = ExecuteComand(ProgramLinesToExe[SubPC]);
if (Status != CONTINUE)
{
if(Status == JUMP)
{
exe(CallList[ProgramLinesToExe[SubPC]->args[0]]->FunctionProgram);
}
else if (Status == CALL)
{
auto test = CallList[ProgramLinesToExe[SubPC]->args[0]]->FunctionProgram;
exe(CallList[ProgramLinesToExe[SubPC]->args[0]]->FunctionProgram);
}
else if (Status == RET)
{
return;
}
else if (Status == END)
{
break;
}
else if (Status == ERROR)
{
std::cout << "Unknown comand! " << ProgramLinesToExe[SubPC]->name;
}
}
SubPC++;
}
}
// Untill this function called second time (recursively) all works good
Program::Status_enum Program::ExecuteComand(Comand_Class* ComandToExe)
{
if (ComandToExe->name == "mov")
{
this->Registers[ComandToExe->args[0]] = GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "inc")
{
this->Registers[ComandToExe->args[0]]++;
}
else if (ComandToExe->name == "dec")
{
this->Registers[ComandToExe->args[0]]--;
}
else if (ComandToExe->name == "add")
{
this->Registers[ComandToExe->args[0]] += GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "sub")
{
this->Registers[ComandToExe->args[0]] -= GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "mil")
{
this->Registers[ComandToExe->args[0]] *= GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "div")
{
//this->Registers[ComandToExe->args[0]] /= GetValue(ComandToExe->args[1]); error
}
else if (ComandToExe->name == "cmp")
{
int x = GetValue(ComandToExe->args[0]);
int y = GetValue(ComandToExe->args[1]);
this->CompareFlugs.Equal = (x == y);
this->CompareFlugs.Less = (x < y);
}
else if (ComandToExe->name == "jne") // if the values of the previous cmp command were not equal.
{
if (this->CompareFlugs.Equal == false)
{
return JUMP;
}
}
else if (ComandToExe->name == "je") // if the values of the previous cmp command were equal.
{
if (this->CompareFlugs.Equal == true)
{
return JUMP;
}
}
else if (ComandToExe->name == "jge") // if x was greater or equal than y in the previous cmp command.
{
if (this->CompareFlugs.Less == false)
{
return JUMP;
}
}
else if (ComandToExe->name == "jg") // f x was greater than y in the previous cmp command.
{
if ((this->CompareFlugs.Less == false) && (this->CompareFlugs.Equal == false))
{
return JUMP;
}
}
else if (ComandToExe->name == "jle") // if x was less or equal than y in the previous cmp command.
{
if ((this->CompareFlugs.Less == true) || (this->CompareFlugs.Equal == true))
{
return JUMP;
}
}
else if (ComandToExe->name == "jl") // if x was less than y in the previous cmp command.
{
if (this->CompareFlugs.Less == true)
{
return JUMP;
}
}
else if (ComandToExe->name == "call")
{
return CALL;
}
else if (ComandToExe->name == "ret")
{
return RET;
}
else if (ComandToExe->name == "end")
{
return END;
}
else if (ComandToExe->name == "msg")
{
MSG = "";
for (unsigned long int i = 0; i < ComandToExe->args.size(); i++)
{
if (ComandToExe->args[i][0] == '\'')
{
MSG += ComandToExe->args[i].substr(1);
}
else
{
MSG += std::to_string(GetValue(ComandToExe->args[i]));
}
}
}
else
{
return ERROR;
}
return CONTINUE;
}
That's all. Also i am new to stackoverflow, so if i did somting very bad, do not judge strictly.
And Here all the code, becase i cut parts i think was usless:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <map>
#include <unordered_map>
#include <regex>
class Comand_Class
{
public:
std::string name;
std::vector<std::string> args;
Comand_Class(std::string Name, std::string arg1 = "", std::string arg2 = "")
{
name = Name;
args.push_back(arg1);
if (!arg2.empty())
{
args.push_back(arg2);
}
}
Comand_Class(std::string Name, std::vector<std::string> Args)
{
name = Name;
args = Args;
}
};
class Function_Class
{
public:
//Comand_Class* ReturnControlTo = NULL;
std::vector<Comand_Class*> FunctionProgram;
};
class Program
{
//int PC = 0;
std::string MSG = "-1";
std::unordered_map <std::string, int> Registers;
std::vector<Comand_Class*> ProgramLines;
std::map<std::string,Function_Class*> CallList;
class CompareFlugs_Class
{
public:
bool Less = false;
bool Equal = false;
}CompareFlugs;
enum Status_enum
{
END = 0,
CALL = 1,
JUMP = 2,
RET = 3,
CONTINUE = 10,
ERROR = -1,
};
const std::string AllowedCommands = "mov inc dev add sub mul div cmp";
public:
std::string Start()
{
exe(this -> ProgramLines );
return MSG;
}
int GetValue(std::string arg)
{
if ('a' <= arg[0] && arg[0] <= 'z')
{
//register
return this->Registers[arg];
}
else
{
int a = std::stoi(arg);
return a;
}
}
void preprocesing(std::string program);
Status_enum ExecuteComand(Comand_Class* ComandToExe);
void exe(std::vector<Comand_Class*> ProgramLinesToExe);
~Program()
{
for (unsigned long i = 0; i < ProgramLines.size(); i++)
{
Comand_Class* del = ProgramLines.at(i);
delete del;
}
for (auto it = CallList.begin(); it != CallList.end(); it++)
{
for (unsigned long i = 0; i < it->second->FunctionProgram.size(); i++)
{
Comand_Class* del = it->second->FunctionProgram.at(i);
delete del;
}
}
// add CallList to clear
}
};
void Program::preprocesing(std::string program)
{
std::string read;
Comand_Class*NewProgramLine;
std::istringstream iss(program);
std::smatch matches;
std::regex Comand(";(.*)|call\\s(.*)|(end|ret)|msg(.*)[;]?|(\\w+):|(j\\w{1,2})\\s+(\\w+)|(\\w+)\\s+(\\w),?\\s?(\\d+|\\w)?");
bool WtireToFunction = false;
std::string ActiveLabel;
Function_Class* NewFunction = nullptr;// = new Function_Class;
while (!iss.eof())
{
std::getline(iss, read);
std::regex_search(read, matches, Comand);
/*
if (matches.size() != 0)
std::cout << "Comand is " << matches[0] << "\n";
*/
// parcing
std::string Coment = matches[1];
std::string CallLabel = matches[2];
std::string End = matches[3];
std::string Message = matches[4]; // may contain a comment in the end
std::string Label = matches[5];
std::string JumpComand = matches[6];
std::string JumpLabel = matches[7];
std::string ComandName = matches[8];
std::string arg1 = matches[9];
std::string arg2 = matches[10];
if ((!ComandName.empty()) && (AllowedCommands.find(ComandName) != std::string::npos))
{
//std::cout << "Comand is " << matches[0] << "\n";
std::cout << "Comand is " << ComandName << " " << arg1 << " " << arg2 << "\n";
NewProgramLine = new Comand_Class (ComandName,arg1,arg2);
//NewProgramLine->name = ComandName;
//NewProgramLine->args.push_back(arg1);
if (WtireToFunction == false)
{
this->ProgramLines.push_back(NewProgramLine);
}
else
{
NewFunction->FunctionProgram.push_back(NewProgramLine);
}
}
else if (!JumpComand.empty()) // && (AllowedJumpComands.find(JumpComand) != std::string::npos))
{
std::cout << "Comand is " << JumpComand << " " << JumpLabel << "\n";
NewProgramLine = new Comand_Class(JumpComand, JumpLabel);
}
else if (!CallLabel.empty()) // on call
{
NewProgramLine = new Comand_Class("call", CallLabel);
this->ProgramLines.push_back(NewProgramLine); // add Call to the ProgramLines
}
else if (!Label.empty()) // Generate New SubProgram
{
WtireToFunction = true;
ActiveLabel = Label;
NewFunction = new Function_Class;
this->CallList[ActiveLabel] = NewFunction;
}
else if (!Message.empty())
{
std::regex MessageParcing("('.*)'|,\\s(\\w)");
std::smatch matches;
std::sregex_iterator it(Message.begin(), Message.end(), MessageParcing), end;
std::vector<std::string> args;
while (it!= end)
{
matches = *it;
if (!matches[1].str().empty())
{
args.push_back(matches[1].str());
}
else if (!matches[2].str().empty())
{
args.push_back(matches[2].str());
}
else
{
std::cout << "Error: cannot format msg\n";
}
it++;
}
NewProgramLine = new Comand_Class("msg",args);
this->ProgramLines.push_back(NewProgramLine);
}
else if (!End.empty())
{
if (End == "end")
{
NewProgramLine = new Comand_Class("end");
this->ProgramLines.push_back(NewProgramLine); // add Call to the ProgramLines
}
if (End == "ret")
{
if (NewFunction == nullptr)
std::cout << "No Call Detected, but ret is found\n";
else
{
NewProgramLine = new Comand_Class("ret");
NewFunction->FunctionProgram.push_back(NewProgramLine);
}
}
}
}
}
Program::Status_enum Program::ExecuteComand(Comand_Class* ComandToExe)
{
if (ComandToExe->name == "mov")
{
this->Registers[ComandToExe->args[0]] = GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "inc")
{
this->Registers[ComandToExe->args[0]]++;
}
else if (ComandToExe->name == "dec")
{
this->Registers[ComandToExe->args[0]]--;
}
else if (ComandToExe->name == "add")
{
this->Registers[ComandToExe->args[0]] += GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "sub")
{
this->Registers[ComandToExe->args[0]] -= GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "mil")
{
this->Registers[ComandToExe->args[0]] *= GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "div")
{
int a = GetValue(ComandToExe->args[1]);
auto t = ComandToExe->args[0];
int b = 6 / 2;
this->MSG = "Hi";
this->Registers[t] = b;
this->Registers.begin()->second = 3;
//this->Registers[ComandToExe->args[0]] /= GetValue(ComandToExe->args[1]);
}
else if (ComandToExe->name == "cmp")
{
int x = GetValue(ComandToExe->args[0]);
int y = GetValue(ComandToExe->args[1]);
this->CompareFlugs.Equal = (x == y);
this->CompareFlugs.Less = (x < y);
}
else if (ComandToExe->name == "jne") // if the values of the previous cmp command were not equal.
{
if (this->CompareFlugs.Equal == false)
{
return JUMP;
}
}
else if (ComandToExe->name == "je") // if the values of the previous cmp command were equal.
{
if (this->CompareFlugs.Equal == true)
{
return JUMP;
}
}
else if (ComandToExe->name == "jge") // if x was greater or equal than y in the previous cmp command.
{
if (this->CompareFlugs.Less == false)
{
return JUMP;
}
}
else if (ComandToExe->name == "jg") // f x was greater than y in the previous cmp command.
{
if ((this->CompareFlugs.Less == false) && (this->CompareFlugs.Equal == false))
{
return JUMP;
}
}
else if (ComandToExe->name == "jle") // if x was less or equal than y in the previous cmp command.
{
if ((this->CompareFlugs.Less == true) || (this->CompareFlugs.Equal == true))
{
return JUMP;
}
}
else if (ComandToExe->name == "jl") // if x was less than y in the previous cmp command.
{
if (this->CompareFlugs.Less == true)
{
return JUMP;
}
}
else if (ComandToExe->name == "call")
{
// написать адресс для возврата в структуру с функциями
return CALL;
}
else if (ComandToExe->name == "ret")
{
return RET;
}
else if (ComandToExe->name == "end")
{
return END;
}
else if (ComandToExe->name == "msg")
{
MSG = "";
for (unsigned long int i = 0; i < ComandToExe->args.size(); i++)
{
if (ComandToExe->args[i][0] == '\'')
{
MSG += ComandToExe->args[i].substr(1);
}
else
{
MSG += std::to_string(GetValue(ComandToExe->args[i]));
}
}
}
else
{
return ERROR;
}
return CONTINUE;
}
void Program::exe(std::vector<Comand_Class*> ProgramLinesToExe)
{
Status_enum Status;
unsigned long int SubPC = 0;
while (SubPC < ProgramLinesToExe.size())
{
Status = ExecuteComand(ProgramLinesToExe[SubPC]);
if (Status != CONTINUE)
{
if(Status == JUMP)
{
exe(CallList[ProgramLinesToExe[SubPC]->args[0]]->FunctionProgram);
}
else if (Status == CALL)
{
auto test = CallList[ProgramLinesToExe[SubPC]->args[0]]->FunctionProgram;
exe(CallList[ProgramLinesToExe[SubPC]->args[0]]->FunctionProgram);
}
else if (Status == RET)
{
return;
}
else if (Status == END)
{
break;
}
else if (Status == ERROR)
{
std::cout << "Unknown comand! " << ProgramLinesToExe[SubPC]->name;
}
}
SubPC++;
}
}
std::string assembler_interpreter(std::string program)
{
Program Main;
Main.preprocesing(program);
return Main.Start();
}

While Overloading The << Operator Was It Correctly Formatted?

I have been getting outputs and sometimes even getting the complete output, however I don't quite understand why I'd be getting said outputs. I want to believe it has something to do with my overloading of said operator "<<" when I perform the operation of outputting the information in the "OpenSea" object. Any leads would be tremendously helpful and thank you for your time!
Note: I included only the necessary files but will update if you think you need more to understand the full scope of my situation. Also there is no need to give me tips on how to format my code as I know a lot of it can be reworked for optimization.
main.cpp
#include "aquaClasses.h"
#include "aquaFish.h"
#include "aquaKillerWhale.h"
#include "aquaPenguin.h"
#include "aquaSea.h"
using namespace std;
int main ()
{
srand (time (NULL));
// Simulation
Sea OpenSea (17, 17);
cout << OpenSea;
}
aquaClasses.h <-- "main" header file
#ifndef AQUACLASSES_H
#define AQUACLASSES_H
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <iomanip>
// Constants
const short int AMT_FISH = 200;
const short int AMT_KWHALES = 2;
const short int AMT_PENGS = 50;
const short int MAX_SIM_INTERATIONS = 10000;
const short int MAX_SEA_GRID = 25;
const short int MAX_SEA_SIZE = 625;
// Functions
template <typename TYPE> int calculate_distance (int obj1_x, int obj1_y,
TYPE obj2);
template <typename TYPE> int calculate_direction (int obj1_x, int obj1_y,
TYPE obj2);
#endif
aquaSea.h
#ifndef AQUASEA_H
#define AQUASEA_H
// Forward Declarations to circumvent circular dependency
class Fish;
class Penguin;
class Killer_Whale;
class Sea
{
private:
char m_grid [MAX_SEA_GRID][MAX_SEA_GRID];
int m_fish;
int m_kwhales;
int m_pengs;
int m_size;
int m_height;
int m_width;
void clear ();
void populate ();
public:
Sea (const int grid_size_height, const int grid_size_width);
friend std::ostream &operator<<(std::ostream &os, Sea obj);
int getHeight () const {return m_height;};
int getWidth () const {return m_width;};
void setCell (int obj_x, int obj_y, char cell_symbol);
// Appropriate accessor/mutator functions
};
#endif
aquaSea.cpp
#include "aquaClasses.h"
#include "aquaFish.h"
#include "aquaPenguin.h"
#include "aquaKillerWhale.h"
#include "aquaSea.h"
using namespace std;
// - [row][column]
Sea::Sea (const int grid_size_height, const int grid_size_width)
{
m_fish = 200;
m_kwhales = 2;
m_pengs = 50;
if ((grid_size_height || grid_size_width) <= 0)
{
if (grid_size_height <= 0 && grid_size_width > 0)
{
m_size = MAX_SEA_GRID * grid_size_width;
m_width = grid_size_width;
m_height = MAX_SEA_GRID;
}
else if (grid_size_width <= 0 && grid_size_height > 0)
{
m_size = MAX_SEA_GRID * grid_size_height;
m_width = MAX_SEA_GRID;
m_height = grid_size_height;
}
else if (grid_size_height <= 0 && grid_size_width <= 0)
{
m_size = MAX_SEA_GRID * MAX_SEA_GRID;
m_height = MAX_SEA_GRID;
m_width = MAX_SEA_GRID;
}
}
else
{
m_size = grid_size_height * grid_size_width;
m_height = grid_size_height;
m_width = grid_size_width;
}
clear ();
populate ();
}
ostream &operator<<(ostream &os, Sea obj)
{
os << "Sea Height: " << obj.m_width << endl << "Sea Wdith: "
<< obj.m_width << endl << "Sea Size: " << obj.m_size
<< endl;
os << "Grid View: " << endl;
for (int i = 0; i < obj.m_height; i++)
{
for (int j = 0; j < obj.m_width; j++)
{
os << obj.m_grid [i][j];
}
os << endl;
}
return os;
}
void Sea::clear ()
{
for (int i = 0; i < m_height; i++)
{
for (int j = 0; j < m_width; j++)
{
m_grid[i][j] = 'O';
}
}
return;
}
void Sea::populate ()
{
// Special simluation variables
bool applyFish = true;
bool applyKWhales = false;
bool applyPengs = false;
int amtFishPop = 35;
int amtKWhalesPop = 2;
int amtPengPop = 20;
int index = 0;
int randGridRow = 0;
int randGridCol = 0;
int totalPop = amtFishPop + amtKWhalesPop + amtPengPop;
Fish arr_fish [AMT_FISH];
Killer_Whale arr_kwhales [AMT_KWHALES];
Penguin arr_pengs [AMT_PENGS];
for (int i = 0; i < totalPop; i++)
{
// Grab random place on grid to apply and check if grid plot is open
randGridRow = rand () % 16;
randGridCol = rand () % 16;
while (m_grid [randGridRow][randGridCol] != 'O')
{
randGridRow = rand () % 16;
randGridCol = rand () % 16;
}
// Populate and Update Fish
if (amtFishPop > 0 && applyFish == true)
{
arr_fish[index].setX (randGridCol);
arr_fish[index].setY (randGridRow);
setCell (arr_fish[index].getX (), arr_fish[index].getY (), 'F');
amtFishPop--;
index++;
}
else if (amtFishPop == 0)
{
applyFish = false;
applyKWhales = true;
index = 0;
}
// Populate and Update Killer Whales
if (amtKWhalesPop > 0 && applyKWhales == true)
{
arr_kwhales[index].setX (randGridCol);
arr_kwhales[index].setY (randGridRow);
setCell (arr_kwhales[index].getX (), arr_kwhales[index].getY (), 'K');
amtKWhalesPop--;
index++;
}
else if (amtKWhalesPop == 0)
{
applyKWhales = false;
applyPengs = true;
index = 0;
}
// Populate and Update Penguins
if (amtPengPop > 0 && applyPengs == true)
{
arr_pengs[index].setX (randGridCol);
arr_pengs[index].setY (randGridRow);
setCell (arr_pengs[index].getX (), arr_pengs[index].getY (), 'P');
amtPengPop--;
index++;
}
else if (amtPengPop == 0)
{
applyPengs = false;
index = 0;
}
}
return;
}
void Sea::setCell (int obj_x, int obj_y, char cell_symbol)
{
m_grid [obj_x][obj_y] = cell_symbol;
return;
}
Types of Output
Wanted:
- Image
Unwanted:
- Image
STOP! AFTER THIS POINT IS OPTIONAL CODE TO FURTHER UNDERSTAND THE SITUATION
Other Code Of Reference If You Want But I Don't Think It's Needed
aquaFish.h
#ifndef AQUAFISH_H
#define AQUAFISH_H
class Fish
{
private:
int m_fish_amt_food;
int m_fish_x;
int m_fish_y;
bool m_fish_alive;
public:
Fish ();
int getX () const {return m_fish_x;};
int getY () const {return m_fish_y;};
void setX (int new_x) {m_fish_x = new_x;};
void setY (int new_y) {m_fish_y = new_y;};
int getFishAmtFood () const {return m_fish_amt_food;};
void move ();
};
#endif
aquaFish.cpp
#include "aquaClasses.h"
#include "aquaFish.h"
using namespace std;
Fish::Fish ()
{
int randNum = rand () % 10 + 1;
m_fish_amt_food = randNum;
m_fish_x = -1;
m_fish_y = -1;
}
void Fish::move ()
{
int randDir = rand () % 8 + 1;
if (randDir == 1)
{
m_fish_y++;
}
else if (randDir == 2)
{
m_fish_x++;
m_fish_y++;
}
else if (randDir == 3)
{
m_fish_x++;
}
else if (randDir == 4)
{
m_fish_x++;
m_fish_y--;
}
else if (randDir == 5)
{
m_fish_y--;
}
else if (randDir == 6)
{
m_fish_x--;
m_fish_y--;
}
else if (randDir == 7)
{
m_fish_x--;
}
else if (randDir == 8)
{
m_fish_x--;
m_fish_y++;
}
return;
}
aquaPenguin.h
#ifndef AQUAPENGUIN_H
#define AQUAPENGUIN_H
// Forward Declarations to circumvent circular dependancy
class Sea;
class Fish;
class Killer_Whale;
class Penguin
{
private:
int m_peng_health; // 0-100
int m_peng_x;
int m_peng_y;
bool m_peng_alive;
public:
Penguin ();
int getX () const {return m_peng_x;};
int getY () const {return m_peng_y;};
void setX (int new_x) {m_peng_x = new_x;};
void setY (int new_y) {m_peng_y = new_y;};
void move (Sea obj_sea, Fish arr_fish [], int arr_fish_size,
Killer_Whale arr_kwhale [], int arr_kwhale_size);
};
#endif
aquaPenguin.cpp
#include "aquaClasses.h"
#include "aquaFish.h"
#include "aquaKillerWhale.h"
#include "aquaPenguin.h"
#include "aquaSea.h"
#include "aquaFunctions.cpp"
using namespace std;
Penguin::Penguin ()
{
m_peng_health = rand () % (81 - 60) + 60;
m_peng_x = -1;
m_peng_y = -1;
m_peng_alive = false;
}
void Penguin::move (Sea obj_sea, Fish arr_fish [], int arr_fish_size,
Killer_Whale arr_kwhale [], int arr_kwhale_size)
{
int actuDistToFish = 8;
int currDistToFish = 0;
int tempMoveX = 0;
int tempMoveY = 0;
int amtMove = 0;
int direction = 0;
int fishIndex = 0;
bool moveAwayKillerWhale = false;
bool fishInRange = false;
// Determine amount of cells to move in sea
if (m_peng_health >= 81 && m_peng_health <= 100)
{
amtMove = 5;
}
else if (m_peng_health >= 61 && m_peng_health <= 80)
{
amtMove = 4;
}
else if (m_peng_health >= 41 && m_peng_health <= 60)
{
amtMove = 3;
}
else if (m_peng_health >= 21 && m_peng_health <= 40)
{
amtMove = 2;
}
else if (m_peng_health >= 1 && m_peng_health <= 20)
{
amtMove = 1;
}
else
{
cout << "Chicken of the sea at: " << m_peng_x << " " << m_peng_y
<< endl;
return;
}
// ADD: Find if any killer whales are near first and if so then penguin just moves away
// Find if any fish are near <-- THIS IS WRONG, YOU NEED TO FIND THE CLOSEST FISH
for (int i = 0; i < arr_fish_size; i++)
{
currDistToFish = calculate_distance (m_peng_x, m_peng_y,
arr_fish[i]);
if (currDistToFish <= 8)
{
if (currDistToFish < actuDistToFish)
{
actuDistToFish = currDistToFish;
fishIndex = i;
fishInRange = true;
}
}
}
// ADD: If fish and whale are found do something, we decide. Otherwise move randomly Peng See Dist 8.0
// ADD Move 1 tick then gauge situation again
for (int k = 0; k < amtMove; k++)
{
if (fishInRange == true && moveAwayKillerWhale == false)
{
tempMoveX = m_peng_x; // temp used for storing before changing
tempMoveY = m_peng_y; // temp used for storing before changing
direction = calculate_direction (m_peng_x, m_peng_y,
arr_fish[fishIndex]);
cout << "Penguin pos before moving: " << m_peng_x << ","
<< m_peng_y << endl;
cout << "Closest Fish pos: " << arr_fish[fishIndex].getX () << ","
<< arr_fish[fishIndex].getY () << endl;
if (m_peng_health == 0)
{
cout << "Chicken of the sea at: " << m_peng_x << " " << m_peng_y
<< endl;
return;
}
if (direction == 1)
{
actuDistToFish--;
m_peng_health--;
tempMoveY++;
}
else if (direction == 2)
{
actuDistToFish--;
m_peng_health--;
tempMoveX++;
tempMoveY++;
}
else if (direction == 3)
{
actuDistToFish--;
m_peng_health--;
tempMoveX++;
}
else if (direction == 4)
{
actuDistToFish--;
m_peng_health--;
tempMoveX++;
tempMoveY--;
}
else if (direction == 5)
{
actuDistToFish--;
m_peng_health--;
tempMoveY--;
}
else if (direction == 6)
{
actuDistToFish--;
m_peng_health--;
tempMoveX--;
tempMoveY--;
}
else if (direction == 7)
{
actuDistToFish--;
m_peng_health--;
tempMoveX--;
}
else if (direction == 8)
{
actuDistToFish--;
m_peng_health--;
tempMoveX--;
tempMoveY++;
}
else
{
cout << "[ERROR]: Penguin direction messed up." << endl;
}
// MODIFY: Lastly check if out of bounds and then move peng
if (tempMoveX > obj_sea.getWidth ()
|| tempMoveX < -(obj_sea.getWidth ()))
{
m_peng_x = m_peng_x; // AKA dont move
m_peng_y = m_peng_y;
}
else if (tempMoveY > obj_sea.getHeight ()
|| tempMoveY < -(obj_sea.getHeight ()))
{
m_peng_x = m_peng_x; // AKA dont move
m_peng_y = m_peng_y;
}
else
{
obj_sea.setCell (m_peng_x, m_peng_y, 'O'); // Delete old cell
m_peng_x = tempMoveX;
m_peng_y = tempMoveY;
obj_sea.setCell (m_peng_x, m_peng_y, 'P'); // Set new cell
}
// Check if peng eats after moving
if (actuDistToFish == 0)
{
// Stop moving
amtMove = 0;
// Eat fish
m_peng_health += arr_fish[fishIndex].getFishAmtFood ();
// ADD: Remove fish from grid
}
cout << "Penguin pos after moving: " << m_peng_x << ","
<< m_peng_y << endl;
}
else if (fishInRange == false && moveAwayKillerWhale == true)
{
}
else if (fishInRange == false && moveAwayKillerWhale == false)
{
// If no fish, movement is random, else it's towards fish how ever many
// step the penguin can go
direction = rand () % 8 + 1;
if (direction == 1)
{
m_peng_y++;
}
else if (direction == 2)
{
m_peng_x++;
m_peng_y++;
}
else if (direction == 3)
{
m_peng_x++;
}
else if (direction == 4)
{
m_peng_x++;
m_peng_y--;
}
else if (direction == 5)
{
m_peng_y--;
}
else if (direction == 6)
{
m_peng_x--;
m_peng_y--;
}
else if (direction == 7)
{
m_peng_x--;
}
else if (direction == 8)
{
m_peng_x--;
m_peng_y++;
}
else
{
cout << "[ERROR]: Penguin random direction messed up." << endl;
}
}
}
return;
}
aquaKillerWhale.h
#ifndef AQUAKILLERWHALE_H
#define AQUAKILLERWHALE_H
class Penguin;
class Killer_Whale
{
private:
int m_kwhale_amt_pengs;
int m_kwhale_x;
int m_kwhale_y;
public:
Killer_Whale ();
int getX () const {return m_kwhale_x;};
int getY () const {return m_kwhale_y;};
void setX (int new_x) {m_kwhale_x = new_x;};
void setY (int new_y) {m_kwhale_y = new_y;};
void move (Penguin arr_peng []);
};
#endif
aquaKillerWhale.cpp
#include "aquaClasses.h"
#include "aquaKillerWhale.h"
#include "aquaPenguin.h"
using namespace std;
Killer_Whale::Killer_Whale ()
{
m_kwhale_x = -1;
m_kwhale_y = -1;
}
void Killer_Whale::move (Penguin arr_peng [])
{
return;
}
aquaFunctions.cpp
#include "aquaClasses.h"
using namespace std;
// Functions
template <typename TYPE>
int calculate_direction (int obj1_x, int obj1_y, TYPE obj2)
{
int calculatedX = obj2.getX () - obj1_x;
int calculatedY = obj2.getY () - obj1_y;
int direction = 0;
if (calculatedX == 0 && calculatedY > 0)
{
direction = 1;
}
else if (calculatedX > 0 && calculatedY > 0)
{
direction = 2;
}
else if (calculatedX > 0 && calculatedY == 0)
{
direction = 3;
}
else if (calculatedX > 0 && calculatedY < 0)
{
direction = 4;
}
else if (calculatedX == 0 && calculatedY < 0)
{
direction = 5;
}
else if (calculatedX < 0 && calculatedY < 0)
{
direction = 6;
}
else if (calculatedX < 0 && calculatedY == 0)
{
direction = 7;
}
else if (calculatedX < 0 && calculatedY > 0)
{
direction = 8;
}
else
{
cout << "[ERROR]: Direction calculation failed." << endl;
}
return direction;
}
template <typename TYPE>
int calculate_distance (int obj1_x, int obj1_y, TYPE obj2)
{
int distance = sqrt ((obj1_x - obj2.getX ())
* (obj1_x - obj2.getX ())
+ (obj1_y - obj2.getY ())
* (obj1_y - obj2.getY ()));
return distance;
}

Program Closing Unexpectedly on Pointer Error

I'm creating an instance of this class from a GUI on_click(). It is supposed to fork() a series of other processes. It runs fine until the end at which point the GUI closes with an error:
free(): invalid pointer: 0x000000000209c608
I understand this means that the program is attempting to access an off-limits memory location. I'm using QT creator, but can't seem to get the debugger to work properly. Here's the code:
scriptfilefeed::scriptfilefeed() { }
scriptfilefeed::scriptfilefeed(vector<vector<vector<string> > > & newFeed, double trimParam1, unsigned int trimParam2)
{
const string min = "<TMIN>";
const string max = "<TMAX>";
bla[0] = "6";
bla[1] = "47";
input.clear();
for (int i = 0; i < newFeed.size(); i++)
{
if (i == 1)
{ // then we first need to run trimlowQ to get arguments
}
input.clear();
for (int j = 0; j < newFeed[i].size(); j++)
{
for (int k = 0; k < newFeed[i][j].size(); k++)
{
curstr = newFeed[i][j][k];
input.push_back(curstr.c_str());
if (i == 1 && input.at(j) == min)
{ // const string comp.for trim parameters
input.pop_back(); // remove last element
input.push_back(bla[0]); // insert this instead
}
if (i == 1 && input.at(j) == max)
{
input.pop_back(); // remove last element
input.push_back(bla[1]);
}
cout << newFeed[i][j][k] << endl;
}
}
while (input.size() <= 13)
{
input.push_back(0);
}
cout << "Starting Process: " << input.at(1) << endl;
child = fork();
int ret;
if (child == 0)
{
ret = execl(input.at(0), input.at(1), input.at(2), input.at(3), input.at(4), input.at(5), input.at(6), input.at(7), input.at(8), input.at(9), input.at(10), input.at(11), input.at(12));
printf("failed... ret=%d\n", ret);
perror("this error occured:");
}
bool childEnded = false;
while (childEnded == false)
{
int status;
int result = waitpid(child, &status, WNOHANG);
if (result == 0)
{
// keep going
}
else if (result == -1)
{
// continue
}
else
{
childEnded = true;
cout << "Process Finished" << endl;
}
}
}
}