compile and run dilemme dillema

This is a discussion on compile and run dilemme dillema within the C++ Programming forums, part of the General Programming Boards category; im having a strange problem here. I compiled this program below on a windows xp machine, and when i ran ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    134

    compile and run dilemme dillema

    im having a strange problem here. I compiled this program below on a windows xp machine, and when i ran it after a certain point in the end it froze on me saying that it has an access violation. so i decided to ssh into a unix account, compiled and ran it there flawlessly, with no troubles!!!! then i booted into knoppix (just to make sure) compiled the program there and again it compiled fine and ran flawlessly with all the correct output.
    WHY IS THIS happening? could someone check it out pls

    I dont know how much this will help, but i wrote it windows with microsoft visual c++ 6.0

    dList.h

    Code:
    #ifndef _H_dList
    #define _H_dList
    #include<cassert>
    
    template<class Type>
    struct nodeType
    {  
       Type info;  
       nodeType<Type> *next;  
       nodeType<Type> * previous;
    };
    
    template<class Type>
    class dList
    {
    public:
       typedef nodeType<Type> *Ptr;
    public:
    	dList();
    	
    	dList(const dList<Type>& otherList);
    	
    	~dList();
    	
    	const dList<Type>& operator=(const dList<Type>& otherList);
    	void push_front(const Type& e);
    	void  push_back(const Type& e); 
    	void insert(Ptr p, const Type& e);
    	void pop_front(); 
    	void pop_back();  
    	void erase(Ptr p);
    	void remove(const Type& e);
    	Type front() const;  
    	Type back() const; 
    	
    	Ptr begin() const
    	{
    		if(!empty())return first;
    
    	return NULL;
    	}
    	
    	Ptr rbegin() const
    	{
    		
    	if(!empty())return last;
    
    	return NULL;
    	}
    	
    	bool empty() const; 
    	int size() const;  
    	void sort();  
    
    protected:
       int count;
       Ptr first;
       Ptr last;
    private:
       void copyList(const dList<Type>& otherList);
    };
    
    
    
    
    //  default constructor
    template<class Type>
    dList<Type>::dList()
    {
    	first=NULL;
    	last=NULL;
    	count=0;
    }
    
    //destructor
    template<class Type>
    dList<Type>::~dList()
    {
    	nodeType<Type>* temp;
    
    	while(first!=NULL)
    	{
    		temp=first;
    		first=first->next;
    		delete temp;
    	}
    	last=NULL;
    	count=0;
    }
    
    //copy constructor
    template<class Type>
    dList<Type>::dList(const dList<Type>& otherList)
    {
    	copyList(otherList);
    }
    
    //overloaded operator =
    template<class Type>
    const dList<Type>& dList<Type>::operator=(const dList<Type>& otherList)
    {
    	if(this != &otherList)
    	{
    		
    	
    		nodeType<Type>* temp;
    
    		while(first!=NULL)
    		{
    			temp=first;
    			first=first->next;
    			delete temp;
    		}
    	
    		last=NULL;
    		count=0;
    		copyList(otherList);
    	}
    	return *this;
    }
    
    
    //returns info in the front (first) node in the list
    //asserts first!= NULL so that if list is empty, program is terminated.
    template<class Type>
    Type dList<Type>::front()const
    {
    	assert(first != NULL);
    
    	return first->info;
    }
    
    //returns info in the back (last) node in the list
    //asserts first!= NULL so that if list is empty, program is terminated.
    template<class Type>
    Type dList<Type>::back()const
    {
    	assert(first != NULL);
    	return last->info;
    }
    
    
    //returns true if list is empty, false otherwise
    template<class Type>
    bool dList<Type>::empty()const
    {
    	return (first == NULL);
    }
    
    //returns current size
    template<class Type>
    int dList<Type>::size()const
    {
    	return count;
    }
    
    //performs a deep copy
    template<class Type>
    void dList<Type>::copyList(const dList<Type>& otherList)
    {
    	count=otherList.count;
    
    	if(count==0)first=last=NULL;
    
    	else
    	{
    		first = new nodeType<Type>;
    		first->info=otherList.first->info;
    
    		nodeType<Type>* new_temp = first;
    		nodeType<Type>* old_temp = otherList.first->next;
    
    		while(old_temp != NULL)
    		{
    			new_temp->next= new nodeType<Type>;
    			new_temp->next->info=old_temp->info;
    			new_temp->next->previous = new_temp;
    			new_temp=new_temp->next;
    			old_temp=old_temp->next;
    		}
    		last=new_temp;
    	}
    }
    
    //sorts list in ascending order
    template<class Type>
    void dList<Type>::sort()
    {
    	for(nodeType<Type>* temp=first;temp != NULL; temp=temp->next)
    	{
    		nodeType<Type>* min = temp;
    
    		for(nodeType<Type>* current = min; curr != NULL; current=current->next)
    		{
    			if(current->info < min->data)min=current;
    		}
    
    		Type temp_var=temp->data;
    		temp->data=min->data;
    		min->data=temp_var;
    	}
    }
    
    //inserts node with info e at the back of the list
    template<class Type>
    void dList<Type>::push_back(const Type& e)
    {
    	nodeType<Type>* temp= new nodeType<Type>;
    	temp->info=e;
    	temp->previous=NULL;
    	temp->next=NULL;
    
    	if(first==NULL && last==NULL)
    	{
    		first=last=temp;
    		count++;
    	}
    
    	else
    	{
    		temp->previous=last;
    		last->next=temp;
    		last=temp;
    		count++;
    	}
    
    	
    }
    
    
    //inserts node with info e at the front of the list
    template<class Type>
    void dList<Type>::push_front(const Type& e)
    {
    	nodeType<Type>* temp= new nodeType<Type>;
    	temp->info=e;
    	temp->previous=NULL;
    	temp->next=NULL;
    	
    	if(first==NULL && last==NULL)
    	{
    		first=last=temp;
    		count++;
    	}
    
    	else
    	{
    		temp->next=first;
    		first->previous=temp;
    		first=temp;
    		count++;
    	}
    }
    //deletes the back node from the list
    //does nothing if list is empty
    template<class Type>
    void dList<Type>::pop_back()
    {
    	if(!empty())
    	{
    		
    
    		if(last==first)
    		{
    			delete first;
    			first=last=NULL;
    		}
    
    		else
    		{
    			last=last->previous;
    			delete last->next;
    			last->next=NULL;
    		}
    		count--;
    	}
    }
    
    //deletes the front node from the list
    //does nothing if list is emoty
    template<class Type>
    void dList<Type>::pop_front()
    {
    	if(!empty())
    	{
    		
    
    		if(first==last)
    		{
    			delete first;
    			last=last=NULL;
    		}
    
    		else
    		{
    			first=first->next;
    			delete first->previous;
    			first->previous=NULL;
    		}
    		count--;
    
    	}
    }
    
    //deletes the node pointed to by p in the list
    //asserts p!=NULL
    template<class Type>
    void dList<Type>::erase(nodeType<Type>* p)
    {
    	assert(p != NULL);
    
    	if(p==first)pop_front();
    	if(p==last)pop_back();
    	else if(p==last && p==first)
    	{
    		delete p;
    		count=0;
    	}
    
    	else
    	{
    		p->previous->next=p->next;
    		p->next->previous=p->previous;
    		delete p;
    		count--;
    	}
    	
    }
    
    //deletes all occurences of e in the list
    template<class Type>
    void dList<Type>::remove(const Type& e)
    {
    	nodeType<Type>* temp;
    	nodeType<Type>* left;
    
    	temp=first;
    	
    
    	while(temp != NULL)
    	{
    		left=temp->next;
    		if(temp->info==e)
    		{
    			erase(temp);
    			
    		}
    		temp=left;
    	}
    }
    
    //inserts noce with e immediately before node pointed by p
    //asserts p!= NULL
    template<class Type>
    void dList<Type>::insert(nodeType<Type>* p, const Type& e)
    {
    	assert( p != NULL);
    
    	nodeType<Type>* temp = new nodeType<Type>;
    	temp->info=e;
    	temp->next=NULL;
    	temp->previous=NULL;
    
    
    	if(p==first && p==last)
    	{
    		first=last=temp;
    	}
    
    	 if(p==first)push_front(e);
       else
       {
            temp->previous=p->previous;
            p->previous->next=temp;
            p->previous = temp;
            temp->next=p;
       }
    	count++;
    }
    #endif
    listdList.cpp

    Code:
    #include <iostream>
    using namespace std;
    #include "dList.h"
    
    int main()
    {  
    	dList<int> dl;
    
       int k = 1;
       dl.push_front(k);
       k++;
       dl.push_back(k);
       k++;
       dl.push_front(k);
       k++;
       //dl now contains the values 3 1 2
       
       cout<<dl.front()<<"  "<<dl.back()<<endl; //outputs 3 2
       
       dl.pop_front();
       
       dl.pop_back();
       //dl now contains 1
       
       cout<<dl.front()<<"  "<<dl.back()<<endl; //outputs 1 1
       
       dl.push_front(k);
       k++;
       dl.push_back(k);
       k++;
       dl.push_front(k);
       k++;
       //dl now contains 6 4 1 5
       
       dList<int>::Ptr p;
       
       p = dl.begin();
       p = p->next; //p points to 4
       dl.insert(p, k);
       dl.erase(p); //dl now contains 6 7 1 5
       dl.push_back(k); //dl now contains 6 7 1 5 7
       for (p = dl.begin(); p != NULL; p = p->next) //outputs 6 7 1 5 7
          cout<<p->info<<" ";
       cout<<endl;
       
       dl.remove(k); //dl now contains 6 1 5
       
       dList<int> dl2(dl); //copy constructor - dl2 now is a copy of dl
       
       dl2.pop_back(); //dl still contains 6 1 5 and dl2 contains 6 1
       
       dList<int> dl3;
       
       dl3 = dl2; //dl3 is now a copy of dl2
       
       dl3.pop_front(); //dl2 still contains 6 1 and dl3 contains 1
       
       for (p = dl.begin(); p != NULL; p = p->next) //outputs 6 1 5 
          cout<<p->info<<" ";
       cout<<endl;
       
       for (p = dl2.begin(); p != NULL; p = p->next) //outputs 6 1
          cout<<p->info<<" ";
       cout<<endl;
       
       for (p = dl3.begin(); p != NULL; p = p->next) //outputs 1 
          cout<<p->info<<" ";
       cout<<endl;
       
       for (p = dl.rbegin(); p != NULL; p = p->previous) //outputs 5 1 6
          cout<<p->info<<" ";
       cout<<endl;
       
       dl3.pop_back(); //dl3 is now empty
       
       cout<<dl.size()<<" "<<dl2.size()<<" "<<dl3.size()<<endl; 
       //outputs 3 2 0
       
       if (!dl.empty()) cout<<"dl is not empty"<<endl;
       if (dl3.empty()) cout<<"dl3 is empty"<<endl;
       
       cout<<"Press the enter key to finish"<<endl;
       char ch;
       cin.get(ch); //pause at end
       return(0);
    
    }

  2. #2
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    You can bet your life savings that I didn't read that code, but here's the (a) problem:
    >>I dont know how much this will help, but i wrote it windows with microsoft visual c++ 6.0<<
    MSVC6 isn't entirely standards compliant... although it's still a darn fine editor/ide/compiler.
    Away.

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    At what point does it give you the error message?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    it stops there on the windows machine

    3 2

    1 1

    6 7 1 5 7

    6 1 5

    6 1

    1 -------> stops here on windows with error.

    5 1 6

    3 2 0

    dl is not empty

    dl3 is empty

    Press the enter key to finish.

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Congratulations on getting things to work well so far. Are you using the debugger in Visual C++? The access violation occurs when outputting dl3 because p points to bad data (0xcdcdcdcd in debug mode in MSVC). Somewhere in the copy constructor and/or the operator= you are not setting the previous and next variables correctly for the first and last items. I think you are just lucky when you run this on Unix that those values don't cause a crash.

    I'm not sure if you don't know how to use the debugging tools or if you just can't quite see the error (which happens to all of us). If you aren't using the watch, stack, variables, etc. debugging windows then it would make errors a lot easier to debug. Learn/ask how to use them. It took only a few minutes to identify the bad data by using those tools.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    thanks a lot for your help man, but i dont not use how to use the debugger. you have any good links to help me out?

    and im still looking into the problem

  7. #7
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Sorry, I don't have any good links. I would guess MSDN would have information on it. I'll also try to give a quick synopsis of how I use the debugger when I get a chance.

    If I was too vague about where the access violation was coming from let me know and I'll be more specific.

  8. #8
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    yeah if you could pls be specific it would be great since this is quite bewildering to me.

  9. #9
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Well, ok, but just this once . I was congratulating you earlier on doing well to fix your errors despite purposefully vague hints. I think its great that you fixed your errors with your own code, even though it took some help to find them.

    As far as this one goes, look in copyList. For each node you are copying in the while loop, you set its info and its previous pointer. The next time through the loop you are setting its next pointer to the next node. But for the last node, where are you setting the next pointer? You aren't, so it is pointing at random memory (or in the case of MSVC in Debug mode 0xcdcdcdcd ... but that's not important right now). Really, the last node's next pointer should always be null.

    The same problem occurs for the first node created. You set its info immediately after creating it, and you set its next pointer inside the while loop, but where do you set the previous pointer for the first node? All you need to do is set those two pointers to NULL in the correct place and your program will run to completion.

  10. #10
    Registered User
    Join Date
    Mar 2003
    Posts
    134
    figured it out, thanks for that jilou you have been great help. I will seriosly look into some online links on how to use the debugger since it can be quite helpful.
    Last edited by noob2c; 09-02-2003 at 08:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 07-13-2008, 09:16 PM
  2. Replies: 5
    Last Post: 10-25-2007, 01:27 PM
  3. Dev-C++ Compile and Run with Pause
    By xmltorrent in forum C++ Programming
    Replies: 12
    Last Post: 03-29-2006, 11:55 PM
  4. Cant compile and run
    By Vente in forum C++ Programming
    Replies: 5
    Last Post: 12-27-2005, 02:11 PM
  5. Replies: 4
    Last Post: 11-14-2001, 10:28 AM

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