I have 2 nodes, called PCB (containing pid, tarq, etc..., and 3 arrays) and I want to set them equal to eachother. To do so, I overloaded the = sign using a helper copy() function.
The header looks like this:
and the source file:Code:class PCB
{
public:
...some functions, including the overloaded = operator...
private:
private:
int pid;
int tarq;
int prio;
int tncpu;
int* cpu_burst;
int* io_burst;
double* avg_cpu_burst;
void copy( const PCB& _pcb );
Now in the driver, the program entirely terminates when I do something like:Code:class PCB
{
PCB::PCB( int _pid, int _tarq, int _prio, int _tncpu, int _cpu_burst[], int _io_burst[] )
{ ... }
PCB::PCB()
{ ... }
...some functions here...
PCB& PCB::operator=( const PCB& _pcb )
{
if (&_pcb != this){
delete [] cpu_burst;
delete [] io_burst;
delete [] avg_cpu_burst;
copy(_pcb);
}
return *this;
}
void PCB::copy( const PCB& _pcb )
{
pid = _pcb.pid;
tarq = _pcb.tarq;
prio = _pcb.prio;
tncpu = _pcb.tncpu;
cpu_burst = new int[MAXSIZE];
io_burst = new int[MAXSIZE];
avg_cpu_burst = new double[MAXSIZE];
for (int i=0; i<=MAXSIZE; i++){
cpu_burst[i] = _pcb.cpu_burst[i];
io_burst[i] = _pcb.io_burst[i];
avg_cpu_burst[i] = _pcb.avg_cpu_burst[i];
}
}
It compiles, links and creates the batch file. But right when executed, it crashes. I am guessing it must because of accessing incorrect memory indeces but I can't see what part of my code is doing that... Btw, I know the problem is from the overloaded operator because when I comment it out (and the assignments in the driver) it works (at least doesn't crash).Code:PCB A;
PCB B;
B = PCB(pid, tarq, prio, tncpu, cpu_burst, io_burst);
A = B;
Any help is greatly needed, and appreciated.