Access violation

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2008
Posts: 1
Reputation: stk123 is an unknown quantity at this point 
Solved Threads: 0
stk123 stk123 is offline Offline
Newbie Poster

Access violation

 
0
  #1
Feb 12th, 2008
Hello,
if I compile this code it crashes at the if statement comparison. Giving me an access violation. it is meant to check, wether the elements stored in the linked list are ascending. I would really appretiate help.
Thanks stk123





#include <fstream.h>
typedef char Item;
class Node {
public:
Item data;
Node* next;
};


int main()
{
typedef Node* NodePtr;
NodePtr hdList = new Node;
NodePtr newNode = new Node;
NodePtr lastPtr = new Node;
NodePtr newPtr = new Node;
NodePtr curentPtr = new Node;
NodePtr temp1 = new Node;
NodePtr temp2 = new Node;
NodePtr temp3 = new Node;


char ch;
char le;
char ch1;
char ch2;

ifstream infile ("input_data_file.txt");
ofstream outfile ("output_file_1.txt");
ifstream infile2 ("delete_data_file.txt");
ofstream outfile2 ("output_file_2.txt");

infile.get(ch);
hdList -> data = ch;
hdList -> next = NULL;
//infile into linked list
while (infile.get(ch))
{
NodePtr newNode = new Node;
newNode -> data = ch;
newNode -> next = hdList;
hdList = newNode;
}

//bouble sort
newPtr = hdList;
curentPtr = hdList;
lastPtr = hdList;
while (lastPtr != NULL)
{
newPtr = curentPtr;
curentPtr = curentPtr -> next;
lastPtr = curentPtr -> next;
ch1 = curentPtr -> data;
ch2 = lastPtr -> data;
if (ch1 > ch2 )
{
ch1 = '3';

}

}

//linked list into outfile
while (hdList!=NULL){
le = hdList -> data;
hdList = hdList -> next;
outfile.put(le);
}


}
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Access violation

 
0
  #2
Feb 12th, 2008
An access violation occurs when you try to access memory that doesn't belong to you.

You need to think a little more carefully about what you are doing with all those variables.

For starters, only mess with one linked list at a time. Also, make sure to get out a piece of paper, draw yourself some variable names and draw little arrows over to little boxes for each node (each node should also have a couple variable names in it: one for the char and one for the next pointer).

Use pencil so you can erase arrows and add and remove things.

Hope this helps.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC