Why is there an exception thrown after inserting a new record for a doubly linked list?
Hello,
I'm making a telephone directory as practice with a double linked list data structure. When trying to select my option to insert a new record after the tail node, I have an exception thrown
Exception thrown: write access violation.
record was nullptr.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | // Exercise13.5_4revised.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <iostream> #include <iomanip> #include <string> using namespace std; struct TeleType { string name; string phoneNo; TeleType *nextaddr; TeleType *prevaddr; }; void populate(TeleType *); // function prototype needed by main() void display(TeleType *); // function prototype needed by main() void remove_head(TeleType *); //function prototype for main() void remove_current(TeleType *); //function prototype for main() void remove_tail(TeleType *); //function prototype for main() void make_list(); void insert(TeleType *); TeleType *findRec(string, TeleType *); TeleType *phead; TeleType *pcurrent; TeleType *ptail; int main() { int i; string search_term; cout << "The list will now be made" << endl; make_list(); cout << "\nThe list consists of the following records:\n"; display(phead); // display the structures cout << "Which record would you like to remove?" << endl; cout << "select 1 if you want to remove the head" << endl; cout << "2 for the middle " << endl; cout << "and 3 for the tail" << endl; cout << ", 4 to insert a new record "<< endl; cout << ", 5 to find a record" << endl; cout << "6 Display records" << endl; cin >> i; cin.ignore(); cout << endl; switch (i) { case 1: remove_head(phead); display(phead); break; case 2: remove_current(pcurrent); display(phead); break; case 3: remove_tail(pcurrent); display(phead); break; case 4: insert(ptail); display(phead); break; case 5: cout << "What are you looking for? " << endl; getline(cin, search_term); cin.ignore(); cout << "The address matching is " << findRec(search_term, phead) << endl; break; case 6: display(phead); break; } system("pause"); return 0; } void make_list() { //Setting the head of the linked list //assign head to a new structure //Call populate function to fill in data //set nextaddr and prevaddr pointers to null //assign tail with head int number_of_records =1; cout << "How many records would you like?" << endl; cin >> number_of_records; TeleType head, current, temp, tail; TeleType *phead = &head; TeleType *pcurrent = ¤t; TeleType *ptemp = &temp; TeleType *ptail = &tail; populate(phead); head.nextaddr = NULL; head.prevaddr = NULL; tail = head; for (int i = 0; i < number_of_records - 1; i++) { pcurrent = new TeleType; populate(pcurrent); pcurrent->nextaddr = NULL; pcurrent->prevaddr = ptail; //ptail->nextaddr = pcurrent; tail = current; } //set up a for loop to make records //make a new node assigned to current structure //call populate function to fill in data members //assign a pointer to to current's nextaddr as NULl //assign pointer prevaddr of current as the pointer of head. //assign tail to current return; } // input a name and phone number void populate(TeleType *record) // record is a pointer to a { // structure of type TeleType cout << "Enter a name: " << endl; getline(cin, record->name); cin.ignore(); cout << "Enter the phone number: "; getline(cin, record->phoneNo); cin.ignore(); return; } void display(TeleType *contents) // contents is a pointer to a { // structure of type TeleType while (contents != NULL) // display until end of linked list { cout << endl << setiosflags(ios::left) << setw(30) << contents->name << setw(20) << contents->phoneNo << setw(20) << contents->prevaddr << setw(20) << contents->nextaddr; contents = contents->nextaddr; } cout << endl; return; } void remove_head(TeleType *record) { return; } void remove_current(TeleType *record) { return; } void remove_tail(TeleType *record) { return; } void insert(TeleType *record) { TeleType *ptr_new_record = new TeleType; //create a new structure, return an address and assign it to a ptr //new record. record->nextaddr = ptr_new_record; //change the nextaddr pointer to point to the new record ptr_new_record->nextaddr = NULL; //assign new null pointer ptr_new_record->prevaddr = record; //assign new pointer of new record to point to the the old tail structure. populate(ptr_new_record); //call populate function to fill in data members of the structure. return; } TeleType *findRec(string term, TeleType *contents) { TeleType *pointer_to_address = phead; while (contents != NULL) { if (term == contents->name) { pointer_to_address = contents; break; } else { cout << "match not found" << endl; } contents = contents->nextaddr; } return pointer_to_address; } | |
In the function make_list, you create four TeleType objects. These are local to that function.
TeleType head, current, temp, tail;
They will all cease to exist when that function finishes. Any pointers to them will be pointing at garbage memory. ptail is a pointer pointing to garbage memory.
Topic archived. No new replies allowed.
Exception Thrown: Write Access Violation. **this** Was Nullptr. Occurred
Source: http://www.cplusplus.com/forum/beginner/228937/
0 Response to "Exception Thrown: Write Access Violation. **this** Was Nullptr. Occurred"
Post a Comment