|
-
July 23rd, 2008, 04:40 PM
#1
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 ^_^
-
July 23rd, 2008, 05:00 PM
#2
Re: Can Any One Help?
First, code tags. Second, run the debugger and let us know where the problem occurs.
-
July 24th, 2008, 03:21 AM
#3
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.
-
July 24th, 2008, 03:21 AM
#4
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 ^_^
-
July 24th, 2008, 03:53 AM
#5
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"
-
July 24th, 2008, 07:19 AM
#6
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.
-
July 24th, 2008, 09:05 AM
#7
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
-
July 24th, 2008, 09:09 AM
#8
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
-
July 24th, 2008, 09:19 AM
#9
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
-
July 24th, 2008, 03:21 PM
#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.
-
July 24th, 2008, 05:02 PM
#11
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.
-
July 24th, 2008, 07:13 PM
#12
Re: Can Any One Help?
 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
-
July 25th, 2008, 03:20 AM
#13
Re: Can Any One Help?
 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.
-
July 25th, 2008, 03:55 AM
#14
Re: Can Any One Help?
 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
-
July 28th, 2008, 03:12 AM
#15
Re: Can Any One Help?
Thanks i'll start on that as soon as i get home from work. =D
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|