overloaded = crashes

This is a discussion on overloaded = crashes within the C++ Programming forums, part of the General Programming Boards category; I have 2 nodes, called PCB (containing pid, tarq, etc..., and 3 arrays) and I want to set them equal ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80

    overloaded = crashes

    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:
    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 );
    and the source file:
    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];
    	}
    }
    Now in the driver, the program entirely terminates when I do something like:
    Code:
    PCB A;
    PCB B;
    B = PCB(pid, tarq, prio, tncpu, cpu_burst, io_burst);
    A = B;
    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).

    Any help is greatly needed, and appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    As a quick fix solution, have you considered using std::vector instead of raw pointers to a dynamic array?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    for (int i=0; i<=MAXSIZE; i++){
    there is no MAXSIZE element in the allocated arrays - memory overrun
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    80
    i fixed the for-loop, still doing the same thing...

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    >> i fixed the for-loop, still doing the same thing...
    Did you make that same mistake in other places? Often overwriting the bounds of an array doesn't show a problem until somewhere else in the code.

    I agree that you should use a vector here. You wouldn't even have to write a copy constructor, the default one would work correctly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program crashes and memory leaks
    By ulillillia in forum Tech Board
    Replies: 1
    Last Post: 05-15-2007, 10:54 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. DEV-C++ made program crashes when run from outside the ide
    By rainmanddw in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2006, 09:27 PM
  4. overloaded >> operator issue...
    By soulredemption in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2005, 10:53 PM
  5. overloaded on overloads
    By emceedee in forum C++ Programming
    Replies: 1
    Last Post: 03-31-2003, 01:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21