Why does my data in a linked list change/corrupt within nested functions?Why does the order in which libraries are linked sometimes cause errors in GCC?printf by given pointer and format string. Issue with floatsWhy does changing 0.1f to 0 slow down performance by 10x?Incorrect result after serializing and deserializing time_t variableHow to overload << cout for a template class?adding two nodes and deleting one while comparing itc++ Linked List losing data between functionsAdding a vertex to a graph using adjacency listCleaning data after exception on class constructorpointer assignments not holding through multiple functions c++
What is this red bug infesting some trees in southern Germany?
Splitting polygons at narrowest part using R?
How many days for hunting?
What happens when there is no available physical memory left for SQL Server?
Round away from zero
How do I stop making people jump at home and at work?
Can there be plants on the dark side of a tidally locked world?
Is torque as fundamental a concept as force?
What's the difference between a share and a stock?
Go for an isolated pawn
How to anonymously report the Establishment Clause being broken?
Global variables and information security
What happens if I double Meddling Mage's 'enter the battlefield' trigger?
How to describe hit point damage without talking about wounds
Is there a name for this metric: TN / (TN + FN)?
Do I need to get a noble in order to win Splendor?
To which airspace does the border of two adjacent airspaces belong to?
Do mortgage points get applied directly to the principal?
What would a biological creature need in order to see the future?
Are kids with daycare background antisocial?
Why did the Joi advertisement trigger K?
Which is the best password hashing algorithm in .NET Core?
MOSFET broke after attaching capacitor bank
One hour 10 min layover in Newark; International -> Domestic connection. Enough time to clear customs?
Why does my data in a linked list change/corrupt within nested functions?
Why does the order in which libraries are linked sometimes cause errors in GCC?printf by given pointer and format string. Issue with floatsWhy does changing 0.1f to 0 slow down performance by 10x?Incorrect result after serializing and deserializing time_t variableHow to overload << cout for a template class?adding two nodes and deleting one while comparing itc++ Linked List losing data between functionsAdding a vertex to a graph using adjacency listCleaning data after exception on class constructorpointer assignments not holding through multiple functions c++
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I am writing code for a discrete time CPU schedule simulator. It simply generates processes and schedules them accordingly. I am currently implementing the FCFS schedule. I understand the nature of a discrete time simulator, but I am have trouble implementing in C++.
The problem occurs in the jump between handleNextEvent()
and generateProcessDeparture()
. At some point the data in my linked-list event queue gets corrupted. (Line 267 in eventQueue.cpp
)
The idea is that handleNextEvent()
pulls the next event from the event queue, an arrival (type 1), and thus generates a departure (type 2) for the same process (PID 1). Everything is good up to this point.
Once control is release by generateProcessDeparture()
and returns to handleNextEvent()
, the original arrival event is deleted, which SHOULD leave only the departure event.
Instead I've got gobbly gook - plus a extra copy of the arrival event somehow. I've researched scope and pointers, but I am a novice to C++, and can't find what I'm doing wrong.
Any insight appreciated. Thank you.
eventQueue.cpp
/*
EVENT TYPES
-1 HEAD: There can be only one head in the linked list of events.
0 NEW: If 0 the event is new and blank. For error checking.
1 arrival: Indicates arrival of a single process.
2 departure: Indicates completion of process. If the event exists within
the event queue, then the simulator has NOT YET accounted for it in
system state or metric report.
*/
#include "eventQueue.h"
#include <iostream>
eventQueue::eventQueue(int dac, int aar, float ast, float q)
// Initiatialize head of event linked list.
eHeadPtr = new event;
eHeadPtr->type = -1;
eHeadPtr->time = -1;
eHeadPtr->next = NULL;
rqHeadPtr = new event;
rqHeadPtr->type = -1;
rqHeadPtr->time = -1;
rqHeadPtr->next = NULL;
defaultArrivalCount = dac;
averageArrivalRate = aar;
averageServiceTime = ast;
averageServiceRate = 1 / ast;
quantum = q;
void eventQueue::runFCFS()
std::cout << "nnrunFCFS()...";
/* generateProcessArrival();
generateProcessArrival();
generateProcessArrival();
generateProcessArrival();
generateProcessArrival();
generateProcessDeparture(*eHeadPtr->next);*/
while(handledProcessCount < defaultArrivalCount)
std::cout << "nnCurrent event queue: ";
printEventQueue();
std::cout << "nCurrent ready queue: ";
printReadyQueue();
std::cout << "nLatestArrivalTime: " << latestArrivalTime;
std::cout << "nNextDepartureTime: " << nextDepartureTime;
if(eventQueueEmpty())
generateProcessArrival();
handleNextEvent();
//std::cout << "nnEvent queue after handleNextEvent:";
//printEventQueue();
while(latestArrivalTime <= nextDepartureTime)
std::cout << "nLatestArrivalTime( " << latestArrivalTime << " ) <= nextDepartureTime( " << nextDepartureTime << " )";
generateProcessArrival();
bool eventQueue::eventQueueEmpty()
if (eHeadPtr->next == NULL)
return true;
return false;
bool eventQueue::readyQueueEmpty()
if (rqHeadPtr->next == NULL)
return true;
return false;
void eventQueue::printEvent(event e)
std::cout << "n [ TYPE: " << e.type << ", TIME: " << e.time << ", PID: "
<< e.proc.id << ", BURST: " << e.proc.cpuBurst << ", A_TIME: " <<
e.proc.arrTime << ", S_TIME: " << e.proc.servTime << ", R_TIME: " <<
e.proc.remTime << ", NEXT: " << e.next << " ] ";
return;
void eventQueue::printEventQueue()
if (eventQueueEmpty())
std::cout << "n [ ]";
else
event* tmpPtr = eHeadPtr;
do
tmpPtr = tmpPtr->next;
printEvent(*tmpPtr);
while(tmpPtr->next != NULL);
tmpPtr = NULL;
return;
void eventQueue::printReadyQueue()
if (readyQueueEmpty())
std::cout << "n [ ]";
else
event* tmpPtr = eHeadPtr;
do
tmpPtr = tmpPtr->next;
printEvent(*tmpPtr);
while(tmpPtr->next != NULL);
tmpPtr = NULL;
return;
double eventQueue::generateBurst()
int intRand = rand() % 100;
float realRand = intRand / 100.0;
double burst = ((-log(1 - realRand))/averageServiceRate);
return burst;
double eventQueue::generateArrivalDiff()
double diffTime;
int intRand = rand() % 100;
double realRand = intRand / 100.0;
double dTime = ((-log(1 - realRand)/averageArrivalRate));
return dTime;
void eventQueue::generateProcessArrival()
std::cout << "nngenerateProcessArrival()...";
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
event* newPtr = new event;
double burst = generateBurst();
double arrivalDiff = generateArrivalDiff();
latestArrivalTime += arrivalDiff;
newProcessID++;
newPtr->type = 1;
newPtr->time = latestArrivalTime;
newPtr->proc.id = newProcessID;
newPtr->proc.arrTime = latestArrivalTime;
newPtr->proc.cpuBurst = burst;
newPtr->proc.servTime = 0;
newPtr->proc.remTime = burst;
std::cout << "nCreated new arrival event:";
printEvent(*newPtr);
if(eventQueueEmpty())
eHeadPtr->next = newPtr;
std::cout << "nEvent queue was empty. Added new arrival event to queue:";
printEventQueue();
else
backPtr = eHeadPtr;
frontPtr = eHeadPtr->next;
while((newPtr->time > frontPtr->time) && (frontPtr->next != NULL))
backPtr = frontPtr;
frontPtr = frontPtr->next;
if ((frontPtr->next == NULL)&&(newPtr->time > frontPtr->time))
frontPtr->next = newPtr;
std::cout << "nAdded new arrival to end of event queue:";
printEventQueue();
else
backPtr->next = newPtr;
newPtr->next = frontPtr;
std::cout << "nAdded new arrival to (center/front) of event queue.";
printEventQueue();
frontPtr = NULL;
backPtr = NULL;
newPtr = NULL;
return;
void eventQueue::generateProcessDeparture(event arr)
std::cout << "nngenerateProcessDeparture()...";
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
event* newPtr = new event;
// Generate departure based on arrival event.
event dep;
dep.type = 2;
dep.proc = arr.proc;
dep.time = arr.proc.arrTime + arr.proc.cpuBurst;
newPtr = &dep;
std::cout << "nCreated new departure event:";
printEvent(*newPtr);
if(eventQueueEmpty())
eHeadPtr->next = newPtr;
std::cout << "nEvent queue was empty. Added new arrival event to queue:";
printEventQueue();
else
backPtr = eHeadPtr;
frontPtr = eHeadPtr->next;
while((newPtr->time > frontPtr->time) && (frontPtr->next != NULL))
backPtr = frontPtr;
frontPtr = frontPtr->next;
if ((frontPtr->next == NULL)&&(newPtr->time > frontPtr->time))
frontPtr->next = newPtr;
std::cout << "nAdded new arrival to end of event queue:";
printEventQueue();
else
backPtr->next = newPtr;
newPtr->next = frontPtr;
std::cout << "nAdded new arrival to (center/front) of event queue.";
printEventQueue();
std::cout << "ngenerateProcessDeparture final check:";
printEventQueue();
frontPtr = NULL;
backPtr = NULL;
newPtr = NULL;
return;
void eventQueue::handleNextEvent()
std::cout << "nnhandleNextEvent()...";
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
frontPtr = eHeadPtr->next;
backPtr = eHeadPtr;
systemClock = frontPtr->time;
if (frontPtr->type == 1)
std::cout << "nArrival event next.";
printEventQueue();
//printEvent(*frontPtr);
if (readyQueueEmpty() && cpuIdle)
std::cout << "nReady queue empty and cpu idle.";
generateProcessDeparture(*frontPtr);
printEventQueue();
backPtr->next = frontPtr->next; // Remove event from event queue.
std::cout << "nnreturn to handleNextEvent()...";
cpuIdle = false;
std::cout << "ncpuIdle: " << cpuIdle;
std::cout << "nCurrent event queue:";
printEventQueue();
else
std::cout << "nReady queue not empty and/or cpu not idle.";
pushReadyQueue(*frontPtr);
backPtr->next = frontPtr->next;
else if (frontPtr->type == 2)
std::cout << "nDeparture event next.";
printEvent(*frontPtr);
if (!readyQueueEmpty())
std::cout << "nReady not empty. Loading next event to CPU.";
event* tmpPtr = rqHeadPtr->next; //Set tmpPtr to first item in readyQueue.
generateProcessDeparture(*tmpPtr); //Creature departure event.
rqHeadPtr->next = tmpPtr->next; //Delete old event.
tmpPtr = NULL;
else
std::cout << "nReady queue empty and cpu idle.";
cpuIdle = true;
backPtr->next = frontPtr->next;
else
std::cout << "nERROR: bad event type in event queue.";
handledProcessCount++;
frontPtr = NULL;
backPtr = NULL;
return;
void eventQueue::pushReadyQueue(event e)
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
while((e.time > frontPtr->time) && (frontPtr->next != NULL))
backPtr = frontPtr;
frontPtr = frontPtr->next;
if ((frontPtr->next == NULL) && (e.time > frontPtr->time))
frontPtr->next = &e;
std::cout << "nnAdded new arrival to end of ready queue:";
printReadyQueue();
else
backPtr->next = &e;
e.next = frontPtr;
std::cout << "nnAdded new arrival to (center/front) of ready queue.";
printReadyQueue();
frontPtr = NULL;
backPtr = NULL;
return;
eventQueue.h
/*
EVENT TYPES
-1 INVALID: if 0 the event is new and blank. For error checking.
0 head: There can be only one head in the linked list of events.
1 arrival: Indicates arrival of a single process.
2 departure: Indicates completion of process. If the event exists within
the event queue, then the simulator has NOT YET accounted for it in
system state or metric report.
*/
#ifndef EVENTQUEUE_H
#define EVENTQUEUE_H
# include <cstddef>
# include <tgmath.h>
# include <ctime>
struct process
int id = -1;
double arrTime = -1;
double cpuBurst = -1;
double servTime = -1;
double remTime = -1;
;
struct event
int type = -1;
double time = -1;
process proc;
event* next = NULL;
;
class eventQueue
private:
event* eHeadPtr;
event* rqHeadPtr;
int defaultArrivalCount;
int handledProcessCount = 0;
int newProcessID = 0;
int averageArrivalRate; // lambda
float averageServiceTime; // Ts
float averageServiceRate; // mu
float quantum; //q
double latestArrivalTime = 0;
double nextDepartureTime = 0;
bool cpuIdle = true;
double systemClock = 0;
public:
eventQueue(const int, int, float, float);
void runFCFS();
bool eventQueueEmpty();
bool readyQueueEmpty();
void printEvent(event);
void printEventQueue();
void printReadyQueue();
void generateProcessArrival();
double generateBurst();
double generateArrivalDiff();
void generateProcessDeparture(event);
void handleNextEvent();
void pushReadyQueue(event);
;
#endif // EVENTQUEUE_H
main.cpp
/*
EVENT TYPES
0 INVALID: if -1 the event is new and blank. For error checking.
1 head: There can be only one head in the linked list of events.
2 arrival: Indicates arrival of a single process.
3 departure: Indicates completion of process. If the event exists within
the event queue, then the simulator has NOT YET accounted for it in
system state or metric report.
*/
#include <iostream>
#include <tgmath.h>
#include "eventQueue.h"
void testInput(int, char*);
int main(int argc, char *argv[])
srand(time(NULL));
char divider[] = "----------------------------------------------------------------------------------------------------------------";
const int DEF_ARR_COUNT= 10;
std::cout << "n" << divider;
std::cout << "nWelcome to the Simulator";
//TEST CMD LINE INPUT
testInput(argc, *argv);
// Setup environment and first default events (arrivals).
//int lambda = std::stoi(argv[2]);
//float Ts = std::stof(argv[3]);
//eventQueue eq(TOTAL_PR, lambda, Ts);
//eq.initDefaultArr();
//eq.printQueue(15);
eventQueue eq(DEF_ARR_COUNT, std::stoi(argv[2]), std::stof(argv[3]), std::stof(argv[4]));
eq.runFCFS();
//eq.printWithoutCount();
std::cout << "n" << divider;
std::cout << "nn";
return 0;
void testInput(int argc, char* argv)
if ((argc < 5)
Here is some example output that I am getting:
anne@laptop:~/Dropbox/College/Current/Opsy/ROUND_4$ g++ main.cpp eventQueue.cpp -o main
anne@laptop:~/Dropbox/College/Current/Opsy/ROUND_4$ ./main 1 10 0.05 0.01
----------------------------------------------------------------------------------------------------------------
Welcome to the Simulator
runFCFS()...
Current event queue:
[ ]
Current ready queue:
[ ]
LatestArrivalTime: 0
NextDepartureTime: 0
generateProcessArrival()...
Created new arrival event:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
Event queue was empty. Added new arrival event to queue:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
handleNextEvent()...
Arrival event next.
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
Ready queue empty and cpu idle.
generateProcessDeparture()...
Created new departure event:
[ TYPE: 2, TIME: 0.359373, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
Added new arrival to end of event queue:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0x7fff5cc50520 ]
[ TYPE: 2, TIME: 0.359373, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
generateProcessDeparture final check:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0x7fff5cc50520 ]
[ TYPE: 2, TIME: 0.359373, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0x7fff5cc50520 ]
[ TYPE: 1556416656, TIME: 4.6627e-310, PID: 1556415824, BURST: 0.00871767, A_TIME: 4.6627e-310, S_TIME: 6.95322e-310, R_TIME: 6.95322e-310, NEXT: 0x55d52b320ec1 ]
Segmentation fault (core dumped)
From what I understand of segmentation faults, its often due to referencing an array or list out of bounds?
c++ pointers scope
|
show 1 more comment
I am writing code for a discrete time CPU schedule simulator. It simply generates processes and schedules them accordingly. I am currently implementing the FCFS schedule. I understand the nature of a discrete time simulator, but I am have trouble implementing in C++.
The problem occurs in the jump between handleNextEvent()
and generateProcessDeparture()
. At some point the data in my linked-list event queue gets corrupted. (Line 267 in eventQueue.cpp
)
The idea is that handleNextEvent()
pulls the next event from the event queue, an arrival (type 1), and thus generates a departure (type 2) for the same process (PID 1). Everything is good up to this point.
Once control is release by generateProcessDeparture()
and returns to handleNextEvent()
, the original arrival event is deleted, which SHOULD leave only the departure event.
Instead I've got gobbly gook - plus a extra copy of the arrival event somehow. I've researched scope and pointers, but I am a novice to C++, and can't find what I'm doing wrong.
Any insight appreciated. Thank you.
eventQueue.cpp
/*
EVENT TYPES
-1 HEAD: There can be only one head in the linked list of events.
0 NEW: If 0 the event is new and blank. For error checking.
1 arrival: Indicates arrival of a single process.
2 departure: Indicates completion of process. If the event exists within
the event queue, then the simulator has NOT YET accounted for it in
system state or metric report.
*/
#include "eventQueue.h"
#include <iostream>
eventQueue::eventQueue(int dac, int aar, float ast, float q)
// Initiatialize head of event linked list.
eHeadPtr = new event;
eHeadPtr->type = -1;
eHeadPtr->time = -1;
eHeadPtr->next = NULL;
rqHeadPtr = new event;
rqHeadPtr->type = -1;
rqHeadPtr->time = -1;
rqHeadPtr->next = NULL;
defaultArrivalCount = dac;
averageArrivalRate = aar;
averageServiceTime = ast;
averageServiceRate = 1 / ast;
quantum = q;
void eventQueue::runFCFS()
std::cout << "nnrunFCFS()...";
/* generateProcessArrival();
generateProcessArrival();
generateProcessArrival();
generateProcessArrival();
generateProcessArrival();
generateProcessDeparture(*eHeadPtr->next);*/
while(handledProcessCount < defaultArrivalCount)
std::cout << "nnCurrent event queue: ";
printEventQueue();
std::cout << "nCurrent ready queue: ";
printReadyQueue();
std::cout << "nLatestArrivalTime: " << latestArrivalTime;
std::cout << "nNextDepartureTime: " << nextDepartureTime;
if(eventQueueEmpty())
generateProcessArrival();
handleNextEvent();
//std::cout << "nnEvent queue after handleNextEvent:";
//printEventQueue();
while(latestArrivalTime <= nextDepartureTime)
std::cout << "nLatestArrivalTime( " << latestArrivalTime << " ) <= nextDepartureTime( " << nextDepartureTime << " )";
generateProcessArrival();
bool eventQueue::eventQueueEmpty()
if (eHeadPtr->next == NULL)
return true;
return false;
bool eventQueue::readyQueueEmpty()
if (rqHeadPtr->next == NULL)
return true;
return false;
void eventQueue::printEvent(event e)
std::cout << "n [ TYPE: " << e.type << ", TIME: " << e.time << ", PID: "
<< e.proc.id << ", BURST: " << e.proc.cpuBurst << ", A_TIME: " <<
e.proc.arrTime << ", S_TIME: " << e.proc.servTime << ", R_TIME: " <<
e.proc.remTime << ", NEXT: " << e.next << " ] ";
return;
void eventQueue::printEventQueue()
if (eventQueueEmpty())
std::cout << "n [ ]";
else
event* tmpPtr = eHeadPtr;
do
tmpPtr = tmpPtr->next;
printEvent(*tmpPtr);
while(tmpPtr->next != NULL);
tmpPtr = NULL;
return;
void eventQueue::printReadyQueue()
if (readyQueueEmpty())
std::cout << "n [ ]";
else
event* tmpPtr = eHeadPtr;
do
tmpPtr = tmpPtr->next;
printEvent(*tmpPtr);
while(tmpPtr->next != NULL);
tmpPtr = NULL;
return;
double eventQueue::generateBurst()
int intRand = rand() % 100;
float realRand = intRand / 100.0;
double burst = ((-log(1 - realRand))/averageServiceRate);
return burst;
double eventQueue::generateArrivalDiff()
double diffTime;
int intRand = rand() % 100;
double realRand = intRand / 100.0;
double dTime = ((-log(1 - realRand)/averageArrivalRate));
return dTime;
void eventQueue::generateProcessArrival()
std::cout << "nngenerateProcessArrival()...";
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
event* newPtr = new event;
double burst = generateBurst();
double arrivalDiff = generateArrivalDiff();
latestArrivalTime += arrivalDiff;
newProcessID++;
newPtr->type = 1;
newPtr->time = latestArrivalTime;
newPtr->proc.id = newProcessID;
newPtr->proc.arrTime = latestArrivalTime;
newPtr->proc.cpuBurst = burst;
newPtr->proc.servTime = 0;
newPtr->proc.remTime = burst;
std::cout << "nCreated new arrival event:";
printEvent(*newPtr);
if(eventQueueEmpty())
eHeadPtr->next = newPtr;
std::cout << "nEvent queue was empty. Added new arrival event to queue:";
printEventQueue();
else
backPtr = eHeadPtr;
frontPtr = eHeadPtr->next;
while((newPtr->time > frontPtr->time) && (frontPtr->next != NULL))
backPtr = frontPtr;
frontPtr = frontPtr->next;
if ((frontPtr->next == NULL)&&(newPtr->time > frontPtr->time))
frontPtr->next = newPtr;
std::cout << "nAdded new arrival to end of event queue:";
printEventQueue();
else
backPtr->next = newPtr;
newPtr->next = frontPtr;
std::cout << "nAdded new arrival to (center/front) of event queue.";
printEventQueue();
frontPtr = NULL;
backPtr = NULL;
newPtr = NULL;
return;
void eventQueue::generateProcessDeparture(event arr)
std::cout << "nngenerateProcessDeparture()...";
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
event* newPtr = new event;
// Generate departure based on arrival event.
event dep;
dep.type = 2;
dep.proc = arr.proc;
dep.time = arr.proc.arrTime + arr.proc.cpuBurst;
newPtr = &dep;
std::cout << "nCreated new departure event:";
printEvent(*newPtr);
if(eventQueueEmpty())
eHeadPtr->next = newPtr;
std::cout << "nEvent queue was empty. Added new arrival event to queue:";
printEventQueue();
else
backPtr = eHeadPtr;
frontPtr = eHeadPtr->next;
while((newPtr->time > frontPtr->time) && (frontPtr->next != NULL))
backPtr = frontPtr;
frontPtr = frontPtr->next;
if ((frontPtr->next == NULL)&&(newPtr->time > frontPtr->time))
frontPtr->next = newPtr;
std::cout << "nAdded new arrival to end of event queue:";
printEventQueue();
else
backPtr->next = newPtr;
newPtr->next = frontPtr;
std::cout << "nAdded new arrival to (center/front) of event queue.";
printEventQueue();
std::cout << "ngenerateProcessDeparture final check:";
printEventQueue();
frontPtr = NULL;
backPtr = NULL;
newPtr = NULL;
return;
void eventQueue::handleNextEvent()
std::cout << "nnhandleNextEvent()...";
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
frontPtr = eHeadPtr->next;
backPtr = eHeadPtr;
systemClock = frontPtr->time;
if (frontPtr->type == 1)
std::cout << "nArrival event next.";
printEventQueue();
//printEvent(*frontPtr);
if (readyQueueEmpty() && cpuIdle)
std::cout << "nReady queue empty and cpu idle.";
generateProcessDeparture(*frontPtr);
printEventQueue();
backPtr->next = frontPtr->next; // Remove event from event queue.
std::cout << "nnreturn to handleNextEvent()...";
cpuIdle = false;
std::cout << "ncpuIdle: " << cpuIdle;
std::cout << "nCurrent event queue:";
printEventQueue();
else
std::cout << "nReady queue not empty and/or cpu not idle.";
pushReadyQueue(*frontPtr);
backPtr->next = frontPtr->next;
else if (frontPtr->type == 2)
std::cout << "nDeparture event next.";
printEvent(*frontPtr);
if (!readyQueueEmpty())
std::cout << "nReady not empty. Loading next event to CPU.";
event* tmpPtr = rqHeadPtr->next; //Set tmpPtr to first item in readyQueue.
generateProcessDeparture(*tmpPtr); //Creature departure event.
rqHeadPtr->next = tmpPtr->next; //Delete old event.
tmpPtr = NULL;
else
std::cout << "nReady queue empty and cpu idle.";
cpuIdle = true;
backPtr->next = frontPtr->next;
else
std::cout << "nERROR: bad event type in event queue.";
handledProcessCount++;
frontPtr = NULL;
backPtr = NULL;
return;
void eventQueue::pushReadyQueue(event e)
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
while((e.time > frontPtr->time) && (frontPtr->next != NULL))
backPtr = frontPtr;
frontPtr = frontPtr->next;
if ((frontPtr->next == NULL) && (e.time > frontPtr->time))
frontPtr->next = &e;
std::cout << "nnAdded new arrival to end of ready queue:";
printReadyQueue();
else
backPtr->next = &e;
e.next = frontPtr;
std::cout << "nnAdded new arrival to (center/front) of ready queue.";
printReadyQueue();
frontPtr = NULL;
backPtr = NULL;
return;
eventQueue.h
/*
EVENT TYPES
-1 INVALID: if 0 the event is new and blank. For error checking.
0 head: There can be only one head in the linked list of events.
1 arrival: Indicates arrival of a single process.
2 departure: Indicates completion of process. If the event exists within
the event queue, then the simulator has NOT YET accounted for it in
system state or metric report.
*/
#ifndef EVENTQUEUE_H
#define EVENTQUEUE_H
# include <cstddef>
# include <tgmath.h>
# include <ctime>
struct process
int id = -1;
double arrTime = -1;
double cpuBurst = -1;
double servTime = -1;
double remTime = -1;
;
struct event
int type = -1;
double time = -1;
process proc;
event* next = NULL;
;
class eventQueue
private:
event* eHeadPtr;
event* rqHeadPtr;
int defaultArrivalCount;
int handledProcessCount = 0;
int newProcessID = 0;
int averageArrivalRate; // lambda
float averageServiceTime; // Ts
float averageServiceRate; // mu
float quantum; //q
double latestArrivalTime = 0;
double nextDepartureTime = 0;
bool cpuIdle = true;
double systemClock = 0;
public:
eventQueue(const int, int, float, float);
void runFCFS();
bool eventQueueEmpty();
bool readyQueueEmpty();
void printEvent(event);
void printEventQueue();
void printReadyQueue();
void generateProcessArrival();
double generateBurst();
double generateArrivalDiff();
void generateProcessDeparture(event);
void handleNextEvent();
void pushReadyQueue(event);
;
#endif // EVENTQUEUE_H
main.cpp
/*
EVENT TYPES
0 INVALID: if -1 the event is new and blank. For error checking.
1 head: There can be only one head in the linked list of events.
2 arrival: Indicates arrival of a single process.
3 departure: Indicates completion of process. If the event exists within
the event queue, then the simulator has NOT YET accounted for it in
system state or metric report.
*/
#include <iostream>
#include <tgmath.h>
#include "eventQueue.h"
void testInput(int, char*);
int main(int argc, char *argv[])
srand(time(NULL));
char divider[] = "----------------------------------------------------------------------------------------------------------------";
const int DEF_ARR_COUNT= 10;
std::cout << "n" << divider;
std::cout << "nWelcome to the Simulator";
//TEST CMD LINE INPUT
testInput(argc, *argv);
// Setup environment and first default events (arrivals).
//int lambda = std::stoi(argv[2]);
//float Ts = std::stof(argv[3]);
//eventQueue eq(TOTAL_PR, lambda, Ts);
//eq.initDefaultArr();
//eq.printQueue(15);
eventQueue eq(DEF_ARR_COUNT, std::stoi(argv[2]), std::stof(argv[3]), std::stof(argv[4]));
eq.runFCFS();
//eq.printWithoutCount();
std::cout << "n" << divider;
std::cout << "nn";
return 0;
void testInput(int argc, char* argv)
if ((argc < 5)
Here is some example output that I am getting:
anne@laptop:~/Dropbox/College/Current/Opsy/ROUND_4$ g++ main.cpp eventQueue.cpp -o main
anne@laptop:~/Dropbox/College/Current/Opsy/ROUND_4$ ./main 1 10 0.05 0.01
----------------------------------------------------------------------------------------------------------------
Welcome to the Simulator
runFCFS()...
Current event queue:
[ ]
Current ready queue:
[ ]
LatestArrivalTime: 0
NextDepartureTime: 0
generateProcessArrival()...
Created new arrival event:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
Event queue was empty. Added new arrival event to queue:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
handleNextEvent()...
Arrival event next.
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
Ready queue empty and cpu idle.
generateProcessDeparture()...
Created new departure event:
[ TYPE: 2, TIME: 0.359373, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
Added new arrival to end of event queue:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0x7fff5cc50520 ]
[ TYPE: 2, TIME: 0.359373, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
generateProcessDeparture final check:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0x7fff5cc50520 ]
[ TYPE: 2, TIME: 0.359373, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0x7fff5cc50520 ]
[ TYPE: 1556416656, TIME: 4.6627e-310, PID: 1556415824, BURST: 0.00871767, A_TIME: 4.6627e-310, S_TIME: 6.95322e-310, R_TIME: 6.95322e-310, NEXT: 0x55d52b320ec1 ]
Segmentation fault (core dumped)
From what I understand of segmentation faults, its often due to referencing an array or list out of bounds?
c++ pointers scope
I see a fewnew
statements but nodelete
statement. That means at least a few memory leaks.
– Etienne de Martel
Mar 28 at 3:29
Can you use std::unique_ptr?
– Captain Giraffe
Mar 28 at 3:30
@EtiennedeMartel I thought that by putting my pointers to NULL near the end of each function would "clean" things up, but I will look into delete. Thank you.
– Anne
Mar 28 at 3:34
@CaptainGiraffe I will research that as a solution as well. Thank you.
– Anne
Mar 28 at 3:34
That would be true if you used smart pointers, or if C++ had a garbage collector, but here you're using raw pointers, which are dumb by nature and can only point to something and cannot manage the lifetime of whatever they point to. In general, manual memory management in C++ is really hazardous, and you should prefer smart pointers whenever possible rather than usingnew
anddelete
directly.
– Etienne de Martel
Mar 28 at 3:38
|
show 1 more comment
I am writing code for a discrete time CPU schedule simulator. It simply generates processes and schedules them accordingly. I am currently implementing the FCFS schedule. I understand the nature of a discrete time simulator, but I am have trouble implementing in C++.
The problem occurs in the jump between handleNextEvent()
and generateProcessDeparture()
. At some point the data in my linked-list event queue gets corrupted. (Line 267 in eventQueue.cpp
)
The idea is that handleNextEvent()
pulls the next event from the event queue, an arrival (type 1), and thus generates a departure (type 2) for the same process (PID 1). Everything is good up to this point.
Once control is release by generateProcessDeparture()
and returns to handleNextEvent()
, the original arrival event is deleted, which SHOULD leave only the departure event.
Instead I've got gobbly gook - plus a extra copy of the arrival event somehow. I've researched scope and pointers, but I am a novice to C++, and can't find what I'm doing wrong.
Any insight appreciated. Thank you.
eventQueue.cpp
/*
EVENT TYPES
-1 HEAD: There can be only one head in the linked list of events.
0 NEW: If 0 the event is new and blank. For error checking.
1 arrival: Indicates arrival of a single process.
2 departure: Indicates completion of process. If the event exists within
the event queue, then the simulator has NOT YET accounted for it in
system state or metric report.
*/
#include "eventQueue.h"
#include <iostream>
eventQueue::eventQueue(int dac, int aar, float ast, float q)
// Initiatialize head of event linked list.
eHeadPtr = new event;
eHeadPtr->type = -1;
eHeadPtr->time = -1;
eHeadPtr->next = NULL;
rqHeadPtr = new event;
rqHeadPtr->type = -1;
rqHeadPtr->time = -1;
rqHeadPtr->next = NULL;
defaultArrivalCount = dac;
averageArrivalRate = aar;
averageServiceTime = ast;
averageServiceRate = 1 / ast;
quantum = q;
void eventQueue::runFCFS()
std::cout << "nnrunFCFS()...";
/* generateProcessArrival();
generateProcessArrival();
generateProcessArrival();
generateProcessArrival();
generateProcessArrival();
generateProcessDeparture(*eHeadPtr->next);*/
while(handledProcessCount < defaultArrivalCount)
std::cout << "nnCurrent event queue: ";
printEventQueue();
std::cout << "nCurrent ready queue: ";
printReadyQueue();
std::cout << "nLatestArrivalTime: " << latestArrivalTime;
std::cout << "nNextDepartureTime: " << nextDepartureTime;
if(eventQueueEmpty())
generateProcessArrival();
handleNextEvent();
//std::cout << "nnEvent queue after handleNextEvent:";
//printEventQueue();
while(latestArrivalTime <= nextDepartureTime)
std::cout << "nLatestArrivalTime( " << latestArrivalTime << " ) <= nextDepartureTime( " << nextDepartureTime << " )";
generateProcessArrival();
bool eventQueue::eventQueueEmpty()
if (eHeadPtr->next == NULL)
return true;
return false;
bool eventQueue::readyQueueEmpty()
if (rqHeadPtr->next == NULL)
return true;
return false;
void eventQueue::printEvent(event e)
std::cout << "n [ TYPE: " << e.type << ", TIME: " << e.time << ", PID: "
<< e.proc.id << ", BURST: " << e.proc.cpuBurst << ", A_TIME: " <<
e.proc.arrTime << ", S_TIME: " << e.proc.servTime << ", R_TIME: " <<
e.proc.remTime << ", NEXT: " << e.next << " ] ";
return;
void eventQueue::printEventQueue()
if (eventQueueEmpty())
std::cout << "n [ ]";
else
event* tmpPtr = eHeadPtr;
do
tmpPtr = tmpPtr->next;
printEvent(*tmpPtr);
while(tmpPtr->next != NULL);
tmpPtr = NULL;
return;
void eventQueue::printReadyQueue()
if (readyQueueEmpty())
std::cout << "n [ ]";
else
event* tmpPtr = eHeadPtr;
do
tmpPtr = tmpPtr->next;
printEvent(*tmpPtr);
while(tmpPtr->next != NULL);
tmpPtr = NULL;
return;
double eventQueue::generateBurst()
int intRand = rand() % 100;
float realRand = intRand / 100.0;
double burst = ((-log(1 - realRand))/averageServiceRate);
return burst;
double eventQueue::generateArrivalDiff()
double diffTime;
int intRand = rand() % 100;
double realRand = intRand / 100.0;
double dTime = ((-log(1 - realRand)/averageArrivalRate));
return dTime;
void eventQueue::generateProcessArrival()
std::cout << "nngenerateProcessArrival()...";
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
event* newPtr = new event;
double burst = generateBurst();
double arrivalDiff = generateArrivalDiff();
latestArrivalTime += arrivalDiff;
newProcessID++;
newPtr->type = 1;
newPtr->time = latestArrivalTime;
newPtr->proc.id = newProcessID;
newPtr->proc.arrTime = latestArrivalTime;
newPtr->proc.cpuBurst = burst;
newPtr->proc.servTime = 0;
newPtr->proc.remTime = burst;
std::cout << "nCreated new arrival event:";
printEvent(*newPtr);
if(eventQueueEmpty())
eHeadPtr->next = newPtr;
std::cout << "nEvent queue was empty. Added new arrival event to queue:";
printEventQueue();
else
backPtr = eHeadPtr;
frontPtr = eHeadPtr->next;
while((newPtr->time > frontPtr->time) && (frontPtr->next != NULL))
backPtr = frontPtr;
frontPtr = frontPtr->next;
if ((frontPtr->next == NULL)&&(newPtr->time > frontPtr->time))
frontPtr->next = newPtr;
std::cout << "nAdded new arrival to end of event queue:";
printEventQueue();
else
backPtr->next = newPtr;
newPtr->next = frontPtr;
std::cout << "nAdded new arrival to (center/front) of event queue.";
printEventQueue();
frontPtr = NULL;
backPtr = NULL;
newPtr = NULL;
return;
void eventQueue::generateProcessDeparture(event arr)
std::cout << "nngenerateProcessDeparture()...";
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
event* newPtr = new event;
// Generate departure based on arrival event.
event dep;
dep.type = 2;
dep.proc = arr.proc;
dep.time = arr.proc.arrTime + arr.proc.cpuBurst;
newPtr = &dep;
std::cout << "nCreated new departure event:";
printEvent(*newPtr);
if(eventQueueEmpty())
eHeadPtr->next = newPtr;
std::cout << "nEvent queue was empty. Added new arrival event to queue:";
printEventQueue();
else
backPtr = eHeadPtr;
frontPtr = eHeadPtr->next;
while((newPtr->time > frontPtr->time) && (frontPtr->next != NULL))
backPtr = frontPtr;
frontPtr = frontPtr->next;
if ((frontPtr->next == NULL)&&(newPtr->time > frontPtr->time))
frontPtr->next = newPtr;
std::cout << "nAdded new arrival to end of event queue:";
printEventQueue();
else
backPtr->next = newPtr;
newPtr->next = frontPtr;
std::cout << "nAdded new arrival to (center/front) of event queue.";
printEventQueue();
std::cout << "ngenerateProcessDeparture final check:";
printEventQueue();
frontPtr = NULL;
backPtr = NULL;
newPtr = NULL;
return;
void eventQueue::handleNextEvent()
std::cout << "nnhandleNextEvent()...";
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
frontPtr = eHeadPtr->next;
backPtr = eHeadPtr;
systemClock = frontPtr->time;
if (frontPtr->type == 1)
std::cout << "nArrival event next.";
printEventQueue();
//printEvent(*frontPtr);
if (readyQueueEmpty() && cpuIdle)
std::cout << "nReady queue empty and cpu idle.";
generateProcessDeparture(*frontPtr);
printEventQueue();
backPtr->next = frontPtr->next; // Remove event from event queue.
std::cout << "nnreturn to handleNextEvent()...";
cpuIdle = false;
std::cout << "ncpuIdle: " << cpuIdle;
std::cout << "nCurrent event queue:";
printEventQueue();
else
std::cout << "nReady queue not empty and/or cpu not idle.";
pushReadyQueue(*frontPtr);
backPtr->next = frontPtr->next;
else if (frontPtr->type == 2)
std::cout << "nDeparture event next.";
printEvent(*frontPtr);
if (!readyQueueEmpty())
std::cout << "nReady not empty. Loading next event to CPU.";
event* tmpPtr = rqHeadPtr->next; //Set tmpPtr to first item in readyQueue.
generateProcessDeparture(*tmpPtr); //Creature departure event.
rqHeadPtr->next = tmpPtr->next; //Delete old event.
tmpPtr = NULL;
else
std::cout << "nReady queue empty and cpu idle.";
cpuIdle = true;
backPtr->next = frontPtr->next;
else
std::cout << "nERROR: bad event type in event queue.";
handledProcessCount++;
frontPtr = NULL;
backPtr = NULL;
return;
void eventQueue::pushReadyQueue(event e)
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
while((e.time > frontPtr->time) && (frontPtr->next != NULL))
backPtr = frontPtr;
frontPtr = frontPtr->next;
if ((frontPtr->next == NULL) && (e.time > frontPtr->time))
frontPtr->next = &e;
std::cout << "nnAdded new arrival to end of ready queue:";
printReadyQueue();
else
backPtr->next = &e;
e.next = frontPtr;
std::cout << "nnAdded new arrival to (center/front) of ready queue.";
printReadyQueue();
frontPtr = NULL;
backPtr = NULL;
return;
eventQueue.h
/*
EVENT TYPES
-1 INVALID: if 0 the event is new and blank. For error checking.
0 head: There can be only one head in the linked list of events.
1 arrival: Indicates arrival of a single process.
2 departure: Indicates completion of process. If the event exists within
the event queue, then the simulator has NOT YET accounted for it in
system state or metric report.
*/
#ifndef EVENTQUEUE_H
#define EVENTQUEUE_H
# include <cstddef>
# include <tgmath.h>
# include <ctime>
struct process
int id = -1;
double arrTime = -1;
double cpuBurst = -1;
double servTime = -1;
double remTime = -1;
;
struct event
int type = -1;
double time = -1;
process proc;
event* next = NULL;
;
class eventQueue
private:
event* eHeadPtr;
event* rqHeadPtr;
int defaultArrivalCount;
int handledProcessCount = 0;
int newProcessID = 0;
int averageArrivalRate; // lambda
float averageServiceTime; // Ts
float averageServiceRate; // mu
float quantum; //q
double latestArrivalTime = 0;
double nextDepartureTime = 0;
bool cpuIdle = true;
double systemClock = 0;
public:
eventQueue(const int, int, float, float);
void runFCFS();
bool eventQueueEmpty();
bool readyQueueEmpty();
void printEvent(event);
void printEventQueue();
void printReadyQueue();
void generateProcessArrival();
double generateBurst();
double generateArrivalDiff();
void generateProcessDeparture(event);
void handleNextEvent();
void pushReadyQueue(event);
;
#endif // EVENTQUEUE_H
main.cpp
/*
EVENT TYPES
0 INVALID: if -1 the event is new and blank. For error checking.
1 head: There can be only one head in the linked list of events.
2 arrival: Indicates arrival of a single process.
3 departure: Indicates completion of process. If the event exists within
the event queue, then the simulator has NOT YET accounted for it in
system state or metric report.
*/
#include <iostream>
#include <tgmath.h>
#include "eventQueue.h"
void testInput(int, char*);
int main(int argc, char *argv[])
srand(time(NULL));
char divider[] = "----------------------------------------------------------------------------------------------------------------";
const int DEF_ARR_COUNT= 10;
std::cout << "n" << divider;
std::cout << "nWelcome to the Simulator";
//TEST CMD LINE INPUT
testInput(argc, *argv);
// Setup environment and first default events (arrivals).
//int lambda = std::stoi(argv[2]);
//float Ts = std::stof(argv[3]);
//eventQueue eq(TOTAL_PR, lambda, Ts);
//eq.initDefaultArr();
//eq.printQueue(15);
eventQueue eq(DEF_ARR_COUNT, std::stoi(argv[2]), std::stof(argv[3]), std::stof(argv[4]));
eq.runFCFS();
//eq.printWithoutCount();
std::cout << "n" << divider;
std::cout << "nn";
return 0;
void testInput(int argc, char* argv)
if ((argc < 5)
Here is some example output that I am getting:
anne@laptop:~/Dropbox/College/Current/Opsy/ROUND_4$ g++ main.cpp eventQueue.cpp -o main
anne@laptop:~/Dropbox/College/Current/Opsy/ROUND_4$ ./main 1 10 0.05 0.01
----------------------------------------------------------------------------------------------------------------
Welcome to the Simulator
runFCFS()...
Current event queue:
[ ]
Current ready queue:
[ ]
LatestArrivalTime: 0
NextDepartureTime: 0
generateProcessArrival()...
Created new arrival event:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
Event queue was empty. Added new arrival event to queue:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
handleNextEvent()...
Arrival event next.
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
Ready queue empty and cpu idle.
generateProcessDeparture()...
Created new departure event:
[ TYPE: 2, TIME: 0.359373, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
Added new arrival to end of event queue:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0x7fff5cc50520 ]
[ TYPE: 2, TIME: 0.359373, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
generateProcessDeparture final check:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0x7fff5cc50520 ]
[ TYPE: 2, TIME: 0.359373, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0x7fff5cc50520 ]
[ TYPE: 1556416656, TIME: 4.6627e-310, PID: 1556415824, BURST: 0.00871767, A_TIME: 4.6627e-310, S_TIME: 6.95322e-310, R_TIME: 6.95322e-310, NEXT: 0x55d52b320ec1 ]
Segmentation fault (core dumped)
From what I understand of segmentation faults, its often due to referencing an array or list out of bounds?
c++ pointers scope
I am writing code for a discrete time CPU schedule simulator. It simply generates processes and schedules them accordingly. I am currently implementing the FCFS schedule. I understand the nature of a discrete time simulator, but I am have trouble implementing in C++.
The problem occurs in the jump between handleNextEvent()
and generateProcessDeparture()
. At some point the data in my linked-list event queue gets corrupted. (Line 267 in eventQueue.cpp
)
The idea is that handleNextEvent()
pulls the next event from the event queue, an arrival (type 1), and thus generates a departure (type 2) for the same process (PID 1). Everything is good up to this point.
Once control is release by generateProcessDeparture()
and returns to handleNextEvent()
, the original arrival event is deleted, which SHOULD leave only the departure event.
Instead I've got gobbly gook - plus a extra copy of the arrival event somehow. I've researched scope and pointers, but I am a novice to C++, and can't find what I'm doing wrong.
Any insight appreciated. Thank you.
eventQueue.cpp
/*
EVENT TYPES
-1 HEAD: There can be only one head in the linked list of events.
0 NEW: If 0 the event is new and blank. For error checking.
1 arrival: Indicates arrival of a single process.
2 departure: Indicates completion of process. If the event exists within
the event queue, then the simulator has NOT YET accounted for it in
system state or metric report.
*/
#include "eventQueue.h"
#include <iostream>
eventQueue::eventQueue(int dac, int aar, float ast, float q)
// Initiatialize head of event linked list.
eHeadPtr = new event;
eHeadPtr->type = -1;
eHeadPtr->time = -1;
eHeadPtr->next = NULL;
rqHeadPtr = new event;
rqHeadPtr->type = -1;
rqHeadPtr->time = -1;
rqHeadPtr->next = NULL;
defaultArrivalCount = dac;
averageArrivalRate = aar;
averageServiceTime = ast;
averageServiceRate = 1 / ast;
quantum = q;
void eventQueue::runFCFS()
std::cout << "nnrunFCFS()...";
/* generateProcessArrival();
generateProcessArrival();
generateProcessArrival();
generateProcessArrival();
generateProcessArrival();
generateProcessDeparture(*eHeadPtr->next);*/
while(handledProcessCount < defaultArrivalCount)
std::cout << "nnCurrent event queue: ";
printEventQueue();
std::cout << "nCurrent ready queue: ";
printReadyQueue();
std::cout << "nLatestArrivalTime: " << latestArrivalTime;
std::cout << "nNextDepartureTime: " << nextDepartureTime;
if(eventQueueEmpty())
generateProcessArrival();
handleNextEvent();
//std::cout << "nnEvent queue after handleNextEvent:";
//printEventQueue();
while(latestArrivalTime <= nextDepartureTime)
std::cout << "nLatestArrivalTime( " << latestArrivalTime << " ) <= nextDepartureTime( " << nextDepartureTime << " )";
generateProcessArrival();
bool eventQueue::eventQueueEmpty()
if (eHeadPtr->next == NULL)
return true;
return false;
bool eventQueue::readyQueueEmpty()
if (rqHeadPtr->next == NULL)
return true;
return false;
void eventQueue::printEvent(event e)
std::cout << "n [ TYPE: " << e.type << ", TIME: " << e.time << ", PID: "
<< e.proc.id << ", BURST: " << e.proc.cpuBurst << ", A_TIME: " <<
e.proc.arrTime << ", S_TIME: " << e.proc.servTime << ", R_TIME: " <<
e.proc.remTime << ", NEXT: " << e.next << " ] ";
return;
void eventQueue::printEventQueue()
if (eventQueueEmpty())
std::cout << "n [ ]";
else
event* tmpPtr = eHeadPtr;
do
tmpPtr = tmpPtr->next;
printEvent(*tmpPtr);
while(tmpPtr->next != NULL);
tmpPtr = NULL;
return;
void eventQueue::printReadyQueue()
if (readyQueueEmpty())
std::cout << "n [ ]";
else
event* tmpPtr = eHeadPtr;
do
tmpPtr = tmpPtr->next;
printEvent(*tmpPtr);
while(tmpPtr->next != NULL);
tmpPtr = NULL;
return;
double eventQueue::generateBurst()
int intRand = rand() % 100;
float realRand = intRand / 100.0;
double burst = ((-log(1 - realRand))/averageServiceRate);
return burst;
double eventQueue::generateArrivalDiff()
double diffTime;
int intRand = rand() % 100;
double realRand = intRand / 100.0;
double dTime = ((-log(1 - realRand)/averageArrivalRate));
return dTime;
void eventQueue::generateProcessArrival()
std::cout << "nngenerateProcessArrival()...";
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
event* newPtr = new event;
double burst = generateBurst();
double arrivalDiff = generateArrivalDiff();
latestArrivalTime += arrivalDiff;
newProcessID++;
newPtr->type = 1;
newPtr->time = latestArrivalTime;
newPtr->proc.id = newProcessID;
newPtr->proc.arrTime = latestArrivalTime;
newPtr->proc.cpuBurst = burst;
newPtr->proc.servTime = 0;
newPtr->proc.remTime = burst;
std::cout << "nCreated new arrival event:";
printEvent(*newPtr);
if(eventQueueEmpty())
eHeadPtr->next = newPtr;
std::cout << "nEvent queue was empty. Added new arrival event to queue:";
printEventQueue();
else
backPtr = eHeadPtr;
frontPtr = eHeadPtr->next;
while((newPtr->time > frontPtr->time) && (frontPtr->next != NULL))
backPtr = frontPtr;
frontPtr = frontPtr->next;
if ((frontPtr->next == NULL)&&(newPtr->time > frontPtr->time))
frontPtr->next = newPtr;
std::cout << "nAdded new arrival to end of event queue:";
printEventQueue();
else
backPtr->next = newPtr;
newPtr->next = frontPtr;
std::cout << "nAdded new arrival to (center/front) of event queue.";
printEventQueue();
frontPtr = NULL;
backPtr = NULL;
newPtr = NULL;
return;
void eventQueue::generateProcessDeparture(event arr)
std::cout << "nngenerateProcessDeparture()...";
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
event* newPtr = new event;
// Generate departure based on arrival event.
event dep;
dep.type = 2;
dep.proc = arr.proc;
dep.time = arr.proc.arrTime + arr.proc.cpuBurst;
newPtr = &dep;
std::cout << "nCreated new departure event:";
printEvent(*newPtr);
if(eventQueueEmpty())
eHeadPtr->next = newPtr;
std::cout << "nEvent queue was empty. Added new arrival event to queue:";
printEventQueue();
else
backPtr = eHeadPtr;
frontPtr = eHeadPtr->next;
while((newPtr->time > frontPtr->time) && (frontPtr->next != NULL))
backPtr = frontPtr;
frontPtr = frontPtr->next;
if ((frontPtr->next == NULL)&&(newPtr->time > frontPtr->time))
frontPtr->next = newPtr;
std::cout << "nAdded new arrival to end of event queue:";
printEventQueue();
else
backPtr->next = newPtr;
newPtr->next = frontPtr;
std::cout << "nAdded new arrival to (center/front) of event queue.";
printEventQueue();
std::cout << "ngenerateProcessDeparture final check:";
printEventQueue();
frontPtr = NULL;
backPtr = NULL;
newPtr = NULL;
return;
void eventQueue::handleNextEvent()
std::cout << "nnhandleNextEvent()...";
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
frontPtr = eHeadPtr->next;
backPtr = eHeadPtr;
systemClock = frontPtr->time;
if (frontPtr->type == 1)
std::cout << "nArrival event next.";
printEventQueue();
//printEvent(*frontPtr);
if (readyQueueEmpty() && cpuIdle)
std::cout << "nReady queue empty and cpu idle.";
generateProcessDeparture(*frontPtr);
printEventQueue();
backPtr->next = frontPtr->next; // Remove event from event queue.
std::cout << "nnreturn to handleNextEvent()...";
cpuIdle = false;
std::cout << "ncpuIdle: " << cpuIdle;
std::cout << "nCurrent event queue:";
printEventQueue();
else
std::cout << "nReady queue not empty and/or cpu not idle.";
pushReadyQueue(*frontPtr);
backPtr->next = frontPtr->next;
else if (frontPtr->type == 2)
std::cout << "nDeparture event next.";
printEvent(*frontPtr);
if (!readyQueueEmpty())
std::cout << "nReady not empty. Loading next event to CPU.";
event* tmpPtr = rqHeadPtr->next; //Set tmpPtr to first item in readyQueue.
generateProcessDeparture(*tmpPtr); //Creature departure event.
rqHeadPtr->next = tmpPtr->next; //Delete old event.
tmpPtr = NULL;
else
std::cout << "nReady queue empty and cpu idle.";
cpuIdle = true;
backPtr->next = frontPtr->next;
else
std::cout << "nERROR: bad event type in event queue.";
handledProcessCount++;
frontPtr = NULL;
backPtr = NULL;
return;
void eventQueue::pushReadyQueue(event e)
// Establish pointers.
event* frontPtr = new event;
event* backPtr = new event;
while((e.time > frontPtr->time) && (frontPtr->next != NULL))
backPtr = frontPtr;
frontPtr = frontPtr->next;
if ((frontPtr->next == NULL) && (e.time > frontPtr->time))
frontPtr->next = &e;
std::cout << "nnAdded new arrival to end of ready queue:";
printReadyQueue();
else
backPtr->next = &e;
e.next = frontPtr;
std::cout << "nnAdded new arrival to (center/front) of ready queue.";
printReadyQueue();
frontPtr = NULL;
backPtr = NULL;
return;
eventQueue.h
/*
EVENT TYPES
-1 INVALID: if 0 the event is new and blank. For error checking.
0 head: There can be only one head in the linked list of events.
1 arrival: Indicates arrival of a single process.
2 departure: Indicates completion of process. If the event exists within
the event queue, then the simulator has NOT YET accounted for it in
system state or metric report.
*/
#ifndef EVENTQUEUE_H
#define EVENTQUEUE_H
# include <cstddef>
# include <tgmath.h>
# include <ctime>
struct process
int id = -1;
double arrTime = -1;
double cpuBurst = -1;
double servTime = -1;
double remTime = -1;
;
struct event
int type = -1;
double time = -1;
process proc;
event* next = NULL;
;
class eventQueue
private:
event* eHeadPtr;
event* rqHeadPtr;
int defaultArrivalCount;
int handledProcessCount = 0;
int newProcessID = 0;
int averageArrivalRate; // lambda
float averageServiceTime; // Ts
float averageServiceRate; // mu
float quantum; //q
double latestArrivalTime = 0;
double nextDepartureTime = 0;
bool cpuIdle = true;
double systemClock = 0;
public:
eventQueue(const int, int, float, float);
void runFCFS();
bool eventQueueEmpty();
bool readyQueueEmpty();
void printEvent(event);
void printEventQueue();
void printReadyQueue();
void generateProcessArrival();
double generateBurst();
double generateArrivalDiff();
void generateProcessDeparture(event);
void handleNextEvent();
void pushReadyQueue(event);
;
#endif // EVENTQUEUE_H
main.cpp
/*
EVENT TYPES
0 INVALID: if -1 the event is new and blank. For error checking.
1 head: There can be only one head in the linked list of events.
2 arrival: Indicates arrival of a single process.
3 departure: Indicates completion of process. If the event exists within
the event queue, then the simulator has NOT YET accounted for it in
system state or metric report.
*/
#include <iostream>
#include <tgmath.h>
#include "eventQueue.h"
void testInput(int, char*);
int main(int argc, char *argv[])
srand(time(NULL));
char divider[] = "----------------------------------------------------------------------------------------------------------------";
const int DEF_ARR_COUNT= 10;
std::cout << "n" << divider;
std::cout << "nWelcome to the Simulator";
//TEST CMD LINE INPUT
testInput(argc, *argv);
// Setup environment and first default events (arrivals).
//int lambda = std::stoi(argv[2]);
//float Ts = std::stof(argv[3]);
//eventQueue eq(TOTAL_PR, lambda, Ts);
//eq.initDefaultArr();
//eq.printQueue(15);
eventQueue eq(DEF_ARR_COUNT, std::stoi(argv[2]), std::stof(argv[3]), std::stof(argv[4]));
eq.runFCFS();
//eq.printWithoutCount();
std::cout << "n" << divider;
std::cout << "nn";
return 0;
void testInput(int argc, char* argv)
if ((argc < 5)
Here is some example output that I am getting:
anne@laptop:~/Dropbox/College/Current/Opsy/ROUND_4$ g++ main.cpp eventQueue.cpp -o main
anne@laptop:~/Dropbox/College/Current/Opsy/ROUND_4$ ./main 1 10 0.05 0.01
----------------------------------------------------------------------------------------------------------------
Welcome to the Simulator
runFCFS()...
Current event queue:
[ ]
Current ready queue:
[ ]
LatestArrivalTime: 0
NextDepartureTime: 0
generateProcessArrival()...
Created new arrival event:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
Event queue was empty. Added new arrival event to queue:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
handleNextEvent()...
Arrival event next.
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
Ready queue empty and cpu idle.
generateProcessDeparture()...
Created new departure event:
[ TYPE: 2, TIME: 0.359373, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
Added new arrival to end of event queue:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0x7fff5cc50520 ]
[ TYPE: 2, TIME: 0.359373, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
generateProcessDeparture final check:
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0x7fff5cc50520 ]
[ TYPE: 2, TIME: 0.359373, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0 ]
[ TYPE: 1, TIME: 0.350656, PID: 1, BURST: 0.00871767, A_TIME: 0.350656, S_TIME: 0, R_TIME: 0.00871767, NEXT: 0x7fff5cc50520 ]
[ TYPE: 1556416656, TIME: 4.6627e-310, PID: 1556415824, BURST: 0.00871767, A_TIME: 4.6627e-310, S_TIME: 6.95322e-310, R_TIME: 6.95322e-310, NEXT: 0x55d52b320ec1 ]
Segmentation fault (core dumped)
From what I understand of segmentation faults, its often due to referencing an array or list out of bounds?
c++ pointers scope
c++ pointers scope
edited Mar 28 at 3:29
Andreas
2,0867 gold badges16 silver badges23 bronze badges
2,0867 gold badges16 silver badges23 bronze badges
asked Mar 28 at 3:18
AnneAnne
12 bronze badges
12 bronze badges
I see a fewnew
statements but nodelete
statement. That means at least a few memory leaks.
– Etienne de Martel
Mar 28 at 3:29
Can you use std::unique_ptr?
– Captain Giraffe
Mar 28 at 3:30
@EtiennedeMartel I thought that by putting my pointers to NULL near the end of each function would "clean" things up, but I will look into delete. Thank you.
– Anne
Mar 28 at 3:34
@CaptainGiraffe I will research that as a solution as well. Thank you.
– Anne
Mar 28 at 3:34
That would be true if you used smart pointers, or if C++ had a garbage collector, but here you're using raw pointers, which are dumb by nature and can only point to something and cannot manage the lifetime of whatever they point to. In general, manual memory management in C++ is really hazardous, and you should prefer smart pointers whenever possible rather than usingnew
anddelete
directly.
– Etienne de Martel
Mar 28 at 3:38
|
show 1 more comment
I see a fewnew
statements but nodelete
statement. That means at least a few memory leaks.
– Etienne de Martel
Mar 28 at 3:29
Can you use std::unique_ptr?
– Captain Giraffe
Mar 28 at 3:30
@EtiennedeMartel I thought that by putting my pointers to NULL near the end of each function would "clean" things up, but I will look into delete. Thank you.
– Anne
Mar 28 at 3:34
@CaptainGiraffe I will research that as a solution as well. Thank you.
– Anne
Mar 28 at 3:34
That would be true if you used smart pointers, or if C++ had a garbage collector, but here you're using raw pointers, which are dumb by nature and can only point to something and cannot manage the lifetime of whatever they point to. In general, manual memory management in C++ is really hazardous, and you should prefer smart pointers whenever possible rather than usingnew
anddelete
directly.
– Etienne de Martel
Mar 28 at 3:38
I see a few
new
statements but no delete
statement. That means at least a few memory leaks.– Etienne de Martel
Mar 28 at 3:29
I see a few
new
statements but no delete
statement. That means at least a few memory leaks.– Etienne de Martel
Mar 28 at 3:29
Can you use std::unique_ptr?
– Captain Giraffe
Mar 28 at 3:30
Can you use std::unique_ptr?
– Captain Giraffe
Mar 28 at 3:30
@EtiennedeMartel I thought that by putting my pointers to NULL near the end of each function would "clean" things up, but I will look into delete. Thank you.
– Anne
Mar 28 at 3:34
@EtiennedeMartel I thought that by putting my pointers to NULL near the end of each function would "clean" things up, but I will look into delete. Thank you.
– Anne
Mar 28 at 3:34
@CaptainGiraffe I will research that as a solution as well. Thank you.
– Anne
Mar 28 at 3:34
@CaptainGiraffe I will research that as a solution as well. Thank you.
– Anne
Mar 28 at 3:34
That would be true if you used smart pointers, or if C++ had a garbage collector, but here you're using raw pointers, which are dumb by nature and can only point to something and cannot manage the lifetime of whatever they point to. In general, manual memory management in C++ is really hazardous, and you should prefer smart pointers whenever possible rather than using
new
and delete
directly.– Etienne de Martel
Mar 28 at 3:38
That would be true if you used smart pointers, or if C++ had a garbage collector, but here you're using raw pointers, which are dumb by nature and can only point to something and cannot manage the lifetime of whatever they point to. In general, manual memory management in C++ is really hazardous, and you should prefer smart pointers whenever possible rather than using
new
and delete
directly.– Etienne de Martel
Mar 28 at 3:38
|
show 1 more comment
1 Answer
1
active
oldest
votes
Question code in this line:
newPtr = &dep;
You should change it to
*newPtr = dep;
Because dep
is defined on the stack buffer, after execute function generateProcessDeparture
, the stack buffer will be overwritten.
Even better, ditchdep
and assign directly to theevent
allocated fornewPtr
earlier.
– user4581301
Mar 28 at 4:12
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389649%2fwhy-does-my-data-in-a-linked-list-change-corrupt-within-nested-functions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Question code in this line:
newPtr = &dep;
You should change it to
*newPtr = dep;
Because dep
is defined on the stack buffer, after execute function generateProcessDeparture
, the stack buffer will be overwritten.
Even better, ditchdep
and assign directly to theevent
allocated fornewPtr
earlier.
– user4581301
Mar 28 at 4:12
add a comment |
Question code in this line:
newPtr = &dep;
You should change it to
*newPtr = dep;
Because dep
is defined on the stack buffer, after execute function generateProcessDeparture
, the stack buffer will be overwritten.
Even better, ditchdep
and assign directly to theevent
allocated fornewPtr
earlier.
– user4581301
Mar 28 at 4:12
add a comment |
Question code in this line:
newPtr = &dep;
You should change it to
*newPtr = dep;
Because dep
is defined on the stack buffer, after execute function generateProcessDeparture
, the stack buffer will be overwritten.
Question code in this line:
newPtr = &dep;
You should change it to
*newPtr = dep;
Because dep
is defined on the stack buffer, after execute function generateProcessDeparture
, the stack buffer will be overwritten.
answered Mar 28 at 4:03
ccxxshowccxxshow
6842 silver badges5 bronze badges
6842 silver badges5 bronze badges
Even better, ditchdep
and assign directly to theevent
allocated fornewPtr
earlier.
– user4581301
Mar 28 at 4:12
add a comment |
Even better, ditchdep
and assign directly to theevent
allocated fornewPtr
earlier.
– user4581301
Mar 28 at 4:12
Even better, ditch
dep
and assign directly to the event
allocated for newPtr
earlier.– user4581301
Mar 28 at 4:12
Even better, ditch
dep
and assign directly to the event
allocated for newPtr
earlier.– user4581301
Mar 28 at 4:12
add a comment |
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55389649%2fwhy-does-my-data-in-a-linked-list-change-corrupt-within-nested-functions%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I see a few
new
statements but nodelete
statement. That means at least a few memory leaks.– Etienne de Martel
Mar 28 at 3:29
Can you use std::unique_ptr?
– Captain Giraffe
Mar 28 at 3:30
@EtiennedeMartel I thought that by putting my pointers to NULL near the end of each function would "clean" things up, but I will look into delete. Thank you.
– Anne
Mar 28 at 3:34
@CaptainGiraffe I will research that as a solution as well. Thank you.
– Anne
Mar 28 at 3:34
That would be true if you used smart pointers, or if C++ had a garbage collector, but here you're using raw pointers, which are dumb by nature and can only point to something and cannot manage the lifetime of whatever they point to. In general, manual memory management in C++ is really hazardous, and you should prefer smart pointers whenever possible rather than using
new
anddelete
directly.– Etienne de Martel
Mar 28 at 3:38