CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jul 2008
    Posts
    10

    Can Any One Help?

    Everytime i attempt to run my program i get this error:

    Unhandled exception at 0x00411725 in test.exe: 0xC0000005: Access violation reading location 0xcccccccc.

    I have read this has something to do with bad ariables but i can't seem to find any =S

    The code is as follows:

    #include "LinkedList.h"
    #include <conio.h> //used for getch();

    LinkedList::LinkedList(){ //constructor

    count = 0;
    Head = NULL;
    Previous = NULL;
    }


    //--------------Function to create new munros(nodes) and populate them with data---------------------------
    void LinkedList:: generateHills(int A,int B,bool C,int D,int E){
    MunroRecord *Next_Node; //declare pointer to node called nextNode
    Next_Node = new MunroRecord; //create new node

    Next_Node->height = A; //set value of hill height to equal parameter A
    Next_Node->distanceFromHome = B; //set value of distance from home to equal parameter B
    Next_Node->climbed = C; //set value of previous climbed to equal parameter C
    Next_Node->timesClimbed = D; //set value of times climbed to equal parameter D
    Next_Node->gridReference = E; //set value of grid reference to equal parameter E

    Next_Node->Head = FirstNode; // give the node’s next field the value currently in FirstNode
    //i.e. the new node now points to the old first node
    FirstNode = Next_Node; //and the FirstNode now points to the new node
    FirstNode->Previous = Next_Node;
    count++; // 1 is added to counter to include the new node in the total number of nodes

    if(LastNode == NULL){ //if there are currently no nodes in the list the linked list’s
    LastNode = Next_Node; // “LastNode” field also points to the new node, as it both the
    } //first and LastNode node.
    }

    MunroRecord* LinkedList::getFirst(){ return FirstNode;}
    //int LinkedList::Munro_Count(){ return count;}





    //--------------------Insertion Sort function to sort linked list via height--------------
    void LinkedList::Insertion_Sort(){
    MunroRecord *current = FirstNode->Head; //2 variables used for searching
    MunroRecord *trailCurrent = FirstNode; //through the linked list
    //now use them to find how much of the list is currently
    //in order, by starting at the FirstNode and continuing so long
    //as the data in one node is greater than that in the previous node
    while(current->height > trailCurrent->height){
    trailCurrent = current;
    current = current->Head;
    }
    MunroRecord *lastInOrder = trailCurrent; //place the information obtained
    MunroRecord *firstOutOfOrder = current; //above into these variables

    //now start at the end of the ordered part of the list and deal one
    //at a time with the unordered nodes
    while(lastInOrder->Head != NULL){
    //first special case - the first out of order node is greater
    //than any in the ordered list and stays where it is, but the pointers
    //"lastInOrder" & "firstOutOfOrder" must now be made to point to
    //the nodes along one
    if(lastInOrder->height < firstOutOfOrder->height){
    lastInOrder = firstOutOfOrder;
    firstOutOfOrder = firstOutOfOrder->Head; //see below - repeat
    } //but no harm done
    else{

    //second special case - the next node must go to the start of the list


    //the following line is highlighted by Visual Studio when it is run--------------

    if(LastNode->height > firstOutOfOrder->height){
    lastInOrder->Head = firstOutOfOrder->Head;
    firstOutOfOrder->Head = FirstNode;
    FirstNode = firstOutOfOrder;
    }
    //otherwise go through the linked list to find where the
    //first out of order node goes and rearrange the links
    //accordingly
    else{
    current = FirstNode->Head;
    trailCurrent = FirstNode;
    while(current->height < firstOutOfOrder->height){
    trailCurrent = current;
    current = current->Head;
    }
    lastInOrder->Head = firstOutOfOrder->Head;
    firstOutOfOrder->Head = current;
    trailCurrent->Head = firstOutOfOrder;
    }

    firstOutOfOrder = lastInOrder->Head; //see above - repeat
    }
    }
    }


    Any help would be greatly appreciated ^_^

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Can Any One Help?

    First, code tags. Second, run the debugger and let us know where the problem occurs.

  3. #3
    Join Date
    Apr 2007
    Location
    Mars NASA Station
    Posts
    1,436

    Re: Can Any One Help?

    This error normally occurs when you have write past boundary of an array/character string.

    I suggest you run the code debug mode and step into it by hand to narrow down the error.

  4. #4
    Join Date
    Jul 2008
    Posts
    10

    Re: Can Any One Help?

    I am pretty certain the problem has do with the variable firstOutOfOrder,

    The line where the problem seems to occur is line 69 which i have highlighted with ******* etc.


    //--------------------Insertion Sort function to sort linked list via height--------------
    void LinkedList::Insertion_Sort(){
    MunroRecord *current = FirstNode->Head; //2 variables used for searching
    MunroRecord *trailCurrent = FirstNode; //through the linked list
    //now use them to find how much of the list is currently
    //in order, by starting at the FirstNode and continuing so long
    //as the data in one node is greater than that in the previous node
    while(current->height > trailCurrent->height){
    trailCurrent = current;
    current = current->Head;
    }
    MunroRecord *lastInOrder = trailCurrent; //place the information obtained
    MunroRecord *firstOutOfOrder = current; //above into these variables

    //now start at the end of the ordered part of the list and deal one
    //at a time with the unordered nodes
    while(lastInOrder->Head != NULL){
    //first special case - the first out of order node is greater
    //than any in the ordered list and stays where it is, but the pointers
    //"lastInOrder" & "firstOutOfOrder" must now be made to point to
    //the nodes along one
    if(lastInOrder->height < firstOutOfOrder->height){
    lastInOrder = firstOutOfOrder;
    firstOutOfOrder = firstOutOfOrder->Head; //see below - repeat
    } //but no harm done
    else{

    //second special case - the next node must go to the start of the list


    //the following line is highlighted by Visual Studio when it is run--------------

    if(LastNode->height > firstOutOfOrder->height){*********************
    lastInOrder->Head = firstOutOfOrder->Head;
    firstOutOfOrder->Head = FirstNode;
    FirstNode = firstOutOfOrder;
    }
    //otherwise go through the linked list to find where the
    //first out of order node goes and rearrange the links
    //accordingly
    else{
    current = FirstNode->Head;
    trailCurrent = FirstNode;
    while(current->height < firstOutOfOrder->height){
    trailCurrent = current;
    current = current->Head;
    }
    lastInOrder->Head = firstOutOfOrder->Head;
    firstOutOfOrder->Head = current;
    trailCurrent->Head = firstOutOfOrder;
    }

    firstOutOfOrder = lastInOrder->Head; //see above - repeat
    }
    }
    }


    Hope that helps ^_^

  5. #5
    Join Date
    May 2002
    Location
    Mumbai
    Posts
    197

    Re: Can Any One Help?

    tag the codes or attach the files...
    Thanks and regards
    Sunnypriya
    "Life is a set of uncertain events. Be careful before you make some statement"

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Can Any One Help?

    What are the values of LastNode and firstOutOfOrder. Most likely one of them is null or unitialized.

    You have to use code tags. Your code is unreadable as posted.

  7. #7
    Join Date
    Jul 2008
    Posts
    10

    Re: Can Any One Help?

    Sorry to such a pain in the *** but how to you mean tag the code?

    Im am a total noob and have no clue =S

  8. #8
    Join Date
    Jan 2003
    Posts
    615

    Re: Can Any One Help?

    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  9. #9
    Join Date
    Jan 2003
    Posts
    615

    Re: Can Any One Help?

    Did you attempt to debug the problem yourself ?

    Check if LastNode and firstOutOfOrder is null.
    Check the value of LastNode->height and firstOutOfOrder->height?

    Please supply definition of MunroRecord.
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  10. #10
    Join Date
    Jul 2008
    Posts
    10

    Re: Can Any One Help?

    Code:
    --------------------------------------------------------------LinkedList.h
    struct MunroRecord{
    	int height;				//height of hill in feet
    	int distanceFromHome;		//distance in miles from home
    	bool climbed;			//whether climbed
        int timesClimbed;			//number of times climbed
    	int gridReference;		//6 digit grid reference
    
    	MunroRecord *Head;
    	MunroRecord *Previous;
    };
    
    
    	//define a node as a structure
    struct Node{		
    	MunroRecord data;			//data about a hill in each node 
    	Node *link;				//declares node pointer called link
    }; 
    
    
    
    class LinkedList{	
    private:
    	MunroRecord *FirstNode;			//declares node pointer called head	
    	MunroRecord *LastNode;			//declares node pointer called last
    	int count;		
    public:
    	LinkedList();	
    	void generateHills(int A,int B,bool C,int D,int E);
    	void insertNodeAtEnd(MunroRecord newinfo);  	
    	int getNodeCount();  
    	//Node* getHead();	
    	MunroRecord *Head;
    	MunroRecord *Previous;
    	MunroRecord *getFirst();
    
    	void Insertion_Sort();		//insertion sort function to sort linked list into order via height
    	
    };
    
    
    
    --------------------------------------------------------------LinkedList.cpp
    #include "LinkedList.h"   
    #include <conio.h>		//used for getch();
    
    LinkedList::LinkedList(){	//constructor
    	
    	count = 0;
        Head = NULL;
        Previous = NULL;
    }
    
    
    //--------------Function to create new munros(nodes) and populate them with data---------------------------
    void LinkedList:: generateHills(int A,int B,bool C,int D,int E){
    	MunroRecord *Next_Node;				//declare pointer to node called nextNode
    	Next_Node = new MunroRecord;				//create new node
    
    	Next_Node->height = A;				//set value of hill height to equal parameter A
    	Next_Node->distanceFromHome = B;	//set value of distance from home to equal parameter B
    	Next_Node->climbed = C;				//set value of previous climbed to equal parameter C
    	Next_Node->timesClimbed = D;		//set value of times climbed to equal parameter D
    	Next_Node->gridReference = E;		//set value of grid reference to equal parameter E
    	
    	Next_Node->Head = FirstNode;		// give the node’s next field the value currently in FirstNode
    											//i.e. the new node now points to the old first node
    	FirstNode = Next_Node;			//and the FirstNode now points to the new node
    	FirstNode->Previous = Next_Node;
    	count++;								// 1 is added to counter to include the new node in the total number of nodes
    	
    if(LastNode == NULL){			//if there are currently no nodes in the list the linked list’s
    		LastNode = Next_Node;	// “LastNode” field also points to the new node, as it both the 
    	}									//first and LastNode node.
    }
    
    MunroRecord* LinkedList::getFirst(){ return FirstNode;}
    //int LinkedList::Munro_Count(){ return count;}
    
    
    
    
    
    //--------------------Insertion Sort function to sort linked list via height--------------
    void LinkedList::Insertion_Sort(){
    	MunroRecord *current = FirstNode->Head;			//2 variables used for searching 
    	MunroRecord *trailCurrent = FirstNode;			//through the linked list
    										//now use them to find how much of the list is currently
    										//in order, by starting at the FirstNode and continuing so long 
    										//as the data in one node is greater than that in the previous node 
    	while(current->height > trailCurrent->height){
    		trailCurrent = current;
    		current = current->Head;
    	}
    	MunroRecord *lastInOrder = trailCurrent;	//place the information obtained
    	MunroRecord *firstOutOfOrder = current;	//above into these variables
    
    										//now start at the end of the ordered part of the list and deal one
    										//at a time with the unordered nodes  
    	while(lastInOrder->Head != NULL){
    			//first special case - the first out of order node is greater
    			//than any in the ordered list and stays where it is, but the pointers
    			//"lastInOrder" & "firstOutOfOrder" must now be made to point to
    			//the nodes along one
    		if(lastInOrder->height < firstOutOfOrder->height){
    			lastInOrder = firstOutOfOrder;
    			firstOutOfOrder = firstOutOfOrder->Head; //see below - repeat
    		}							     //but no harm done											 
    		else{
    			 
    			//second special case - the next node must go to the start of the list
    			if(LastNode->height > firstOutOfOrder->height){ 
    				lastInOrder->Head = firstOutOfOrder->Head;
    				firstOutOfOrder->Head = FirstNode;
    				FirstNode = firstOutOfOrder;
    			}
    			//otherwise go through the linked list to find where the 
    			//first out of order node goes and rearrange the links
    			//accordingly
    			else{
    				current = FirstNode->Head;
    				trailCurrent = FirstNode;
    				while(current->height < firstOutOfOrder->height){
    					trailCurrent = current;
    					current = current->Head;
    				}
    				lastInOrder->Head = firstOutOfOrder->Head;
    				firstOutOfOrder->Head = current;
    				trailCurrent->Head = firstOutOfOrder;
    			}
    		
    			firstOutOfOrder = lastInOrder->Head;    //see above - repeat
    		}
    	}
    }
    The problem occurs at line 69 (highlighted in red) , or at least thats where it breaks from debugging.
    This was working before but then i started fiddling with it and now i have no idea what is up with it.

    There is also a main.cpp but i didn't include as i didn't think it would be relavent to the problem.
    Last edited by cosmicsafari; July 24th, 2008 at 03:34 PM.

  11. #11
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Can Any One Help?

    You've been asked to examine a couple of variables in your debugger. It's frustrating when advice is given but ignored.

  12. #12
    Join Date
    Aug 2005
    Location
    LI, NY
    Posts
    576

    Re: Can Any One Help?

    Quote Originally Posted by GCDEF
    What are the values of LastNode and firstOutOfOrder. Most likely one of them is null or unitialized.
    In this case it is an unitialized pointer. 0xCC is the bit pattern used by Microsoft compilers to indicate unitialized data on the stack (in debug builds).
    - Alon

  13. #13
    Join Date
    Jul 2008
    Posts
    10

    Re: Can Any One Help?

    Quote Originally Posted by GCDEF
    You've been asked to examine a couple of variables in your debugger. It's frustrating when advice is given but ignored.
    yeah sorry about that i forgot, assuming i done it correctly it said they were all NULL.

  14. #14
    Join Date
    Jan 2003
    Posts
    615

    Re: Can Any One Help?

    Quote Originally Posted by cosmicsafari
    yeah sorry about that i forgot, assuming i done it correctly it said they were all NULL.
    Are you saying both LastNode and firstOutOfOrder are null ? That would explain why the application crashes. Now I suggest you try to debug the application to figure out why these pointers are null. Either step through the code via visual studio or use std::cout to print debug information.
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  15. #15
    Join Date
    Jul 2008
    Posts
    10

    Re: Can Any One Help?

    Thanks i'll start on that as soon as i get home from work. =D

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured