| | |
Access violation
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2008
Posts: 1
Reputation:
Solved Threads: 0
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);
}
}
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);
}
}
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.
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.
![]() |
Similar Threads
- Access violation reading location 0xcccccccc (C++)
- Access violation error in borland c++ (C++)
- Access Violation (C)
- Access violation in C (C)
- access violation at address 0055E531 in module (Windows NT / 2000 / XP)
- Unhandled exception in IEXPLORER.EXE (SHDOCVW.DLL): 0xC0000005: Access Violation (Web Browsers)
- access violation at 0x00000024 (Windows NT / 2000 / XP)
- Access Violation (Segmentation Fault) + atol (C++)
- unhandaled exception,0X000005:access violation (C)
Other Threads in the C++ Forum
- Previous Thread: Is there a way to run two or more functions in C++? (For a game in console mode)
- Next Thread: text file to linked list
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






