Thread: overloaded = crashes

  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
    28,413
    As a quick fix solution, have you considered using std::vector instead of raw pointers to a dynamic array?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    for (int i=0; i<=MAXSIZE; i++){
    there is no MAXSIZE element in the allocated arrays - memory overrun
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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,366
    >> 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, 05: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, 10: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, 02:14 AM