C++ Access violation reading location in linked list -


i have lately been trying solve this reddit challenge(problem's part 2):

description

thanks list made me, thoughts way more organised! i've got few problems though thought might able with? >sometimes put wrong information in list item. maybe prevent i'd able modify/update list item? that's not problem though, when there 50+ items gets kind of hard work way through. think maybe add ability categorise items? obviously, if have that, i'd able view category! oh , finally, few of great , did last time there way can somehow make list retain state don't have re-type everytime turn computer on again? newest to-do list should capable of following functionality: modifying existing list item able give list item category. list item should able take arbitrary amount of categorys view category - list items should able sorted , output category make easier wade through submissions retain state thanks!

formal inputs & outputs

output description

any output created should user-friendly. when i'm viewing to-do list, should able discern 1 list item another. examples

(don't take literally, how it) categorisation

input:

category output

input: output: ----programming---- - pixel not pixel not pixel - scheme programming language - memory in c - haskell's school of music - algorithmic symphonies 1 line of code

----music---- - modes in folk music - use of melodic minor scale - haskell's school of music - algorithmic symphonies 1 line of code

----music & programming---- - haskell's school of music - algorithmic symphonies 1 line of code

modifying item

updateitem('create sine waves in c', 'create sine waves in python'); //the item has changed 'create sine waves in c' 'create sine waves in python'. should reflected in viewlist function/method have created.

the error i'm having 'access violation reading location '. that's because in list<std::string> realized while debugging inside of list's nodes data = <error reading characters of string>, before getting out of todolist::additem() data = "take shower"(or whatever data has equal to).

i hope i'm explaining problem correctly.

#include <iostream> #include <string> #include <vector>  template<class t> struct node {     t data;     node* next = nullptr;     node();     node(t item); };  template<class t> node<t>::node() { }  template<class t> node<t>::node(t item) : data(item) { }  template<class t> class list {     node<std::string>* head;     node<std::string>* tail;     int size;  public:     list();     ~list();      void additem(t item);     void deleteitem(t item);     void display() const; }; template<class t> list<t>::list() : size(0) {     head = new node<std::string>();     tail = head; }  template<class t> list<t>::~list() {     auto* temp = head;     (int = 0; < size; ++i){         head = head->next;         delete temp;          temp = head;     } }a  template<class t> void list<t>::additem(t item) {     tail->data = item;     tail = tail->next = new node<std::string>;     size++; }  template<class t> void list<t>::deleteitem(t item) {     auto* temp = head;     (int = 0; < size; ++i){         if (temp->data == item){             if (temp == head){                 auto* h2 = head->next;                 delete temp;                 temp = 0;                 head = h2;             }              else if (temp == tail){                 delete temp;                 temp = 0;             }              else{                 temp->data = temp->next->data;                 auto* te = temp->next->next;                 delete temp->next;                 temp->next = 0;                 temp->next = te;             }              --size;             break;         }          temp = temp->next;     } }  template <class t> void list<t>::display() const {     auto* temp = head;     (int = 0; < size; ++i)     {         std::cout << "- " << temp->data;         temp = temp->next;     } }  class category {     std::string name;     list<std::string> list;  public:     category(std::string name);      void additemtocategory(std::string item);     void doneitemincategory(std::string item);     void displaycategory() const;      std::string getname() const; };  category::category(std::string _name) : name(_name) { }  void category::additemtocategory(std::string item) {     list.additem(item); }  void category::doneitemincategory(std::string item) {     list.deleteitem(item); }  void category::displaycategory() const {     list.display(); }  std::string category::getname() const {     return name;  }  class todolist  {     std::vector<category> categories;  public:     todolist();      void additem(std::string item, std::string category);      void viewlist(); };  todolist::todolist() { }  void todolist::additem(std::string item, std::string category) {     if (!categories.size()){         category newcategory(category);         newcategory.additemtocategory(item);          categories.push_back(newcategory);     }     else{         (auto& cate : categories){             if (cate.getname() == category){                 cate.additemtocategory(item);                 return;             }         }          category newcategory(category);         newcategory.additemtocategory(item);          categories.push_back(newcategory);     } }  void todolist::viewlist() {     (const auto& cate : categories){         std::cout << "------" << cate.getname() << "------\n";         cate.displaycategory();     }     std::cout << "\n" <<std::endl ; }  int main() {     todolist list;      list.additem("take shower", "x");     list.additem("go work", "x");     list.viewlist();      list.additem("buy new phone", "y");     list.viewlist();      std::cin.ignore(2); } 

i suggest read rule of 5. basically, defining destructor list not declaring copy/move constructors, you've made doing kind of copy/move dangerous , put list in unusable state. specifically, @ this:

if (!categories.size()){     category newcategory(category);     newcategory.additemtocategory(item);      categories.push_back(newcategory); } 

what happens here? construct newcategory, copy categories, destroy it. since haven't defined copy constructors, shallow copy - copies pointer values over, not doing memory point to. happens when destroy it? ~category() runs, calls ~list(), deletes of nodes. you've deallocated memory, stuff in categories points garbage.

to fix this, can define copy constructors list allocate new nodes , copy on data. ideally, you'd define move constructors well, allow safely , without memory allocations.


Comments