Click to See Complete Forum and Search --> : Can Any One Help?


cosmicsafari
July 23rd, 2008, 04:40 PM
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 ^_^

Lindley
July 23rd, 2008, 05:00 PM
First, code tags. Second, run the debugger and let us know where the problem occurs.

Peter_APIIT
July 24th, 2008, 03:21 AM
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.

cosmicsafari
July 24th, 2008, 03:21 AM
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 ^_^

SunnyPriya
July 24th, 2008, 03:53 AM
tag the codes or attach the files...

GCDEF
July 24th, 2008, 07:19 AM
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.

cosmicsafari
July 24th, 2008, 09:05 AM
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

laasunde
July 24th, 2008, 09:09 AM
Code tags (http://www.codeguru.com/forum/misc.php?do=bbcode#code)

laasunde
July 24th, 2008, 09:19 AM
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.

cosmicsafari
July 24th, 2008, 03:21 PM
--------------------------------------------------------------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.

GCDEF
July 24th, 2008, 05:02 PM
You've been asked to examine a couple of variables in your debugger. It's frustrating when advice is given but ignored.

Hermit
July 24th, 2008, 07:13 PM
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).

cosmicsafari
July 25th, 2008, 03:20 AM
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.

laasunde
July 25th, 2008, 03:55 AM
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.

cosmicsafari
July 28th, 2008, 03:12 AM
Thanks i'll start on that as soon as i get home from work. =D

cosmicsafari
July 28th, 2008, 01:47 PM
It seems to be LastNode which is problematic,

CXX0030 expression cannot be evaluated

im going through it trying to identify the source of this at the moment.