简体   繁体   中英

Segmentation Fault in Custom map class

Working on implementing a custom map class in C++ for a class assignment. The program compiles fine, but as soon as the program tries to display the keys, it crashes.

Map.h file

#ifndef MAP_H
#define MAP_H
#include<string>

template<typename K, typename V>
class Map {

  public:
   Map();
   ~Map();
   V& operator[](const K&);
   void traverse(void(*f)(const K&, const V&));

  private:
   class Node{             //Node Class                                                                                                                                    
     public:
     Node(K k=K(0), V v=V(0), Node* l = nullptr, Node* r = nullptr) :
      Key(k), Value(v), left(l), right(r) {}
      K Key;
      V Value;
      Node* left;
      Node* right;
   };
   Node* head;
   //Helper Functions                                                                                                                                                      
   //                                                                                                                                                                      
   void traverseHelp(void(*f)(const K&, const V&), Node*);
   Node* find(Node*, const K&) const;
   Node* insert(Node*, const K&);
   void deleteNodes(Node*);
};

#include"./Map.cc"
#endif

Map.cc file

#include<iostream>
#include"Map.h"
#include<string>
using namespace std;


//Map Constructor & Destructor                                                                                                                                             
//                                                                                                                                                                         
template<typename K, typename V>
Map<K,V>::Map(){
   head = nullptr;
}

template<typename K, typename V>
Map<K,V>::~Map(){
   deleteNodes(head);
}


template<typename K, typename V>
void Map<K,V>::deleteNodes(Node* start){
   if(start->left){
      deleteNodes(start->left);
      start->left=nullptr;
   }
   if(start->right){
      deleteNodes(start->right);
      start->right=nullptr;
   }
   if(start->left==nullptr && start->right==nullptr){
      delete start;
      start=nullptr;
   }
}


//operator[] and Helpers                                                                                                                                                   
//                                                                                                                                                                         
template<typename K, typename V>
V& Map<K,V>::operator[](const K& keyIn)
{
   Node* temp= find(head,keyIn);
   if(temp == nullptr)
   {
      temp = insert(head,keyIn);
      cout << "inserted " << temp->Key << endl;
   }
   cout << "returning " << temp->Value << endl;
   return temp->Value;
}



template<typename K, typename V>
typename Map<K,V>::Node* Map<K,V>::insert(Node* start, const K& keyIn){
   Node* ptr = start;
   if(ptr==nullptr)
   {
      ptr = new Node(keyIn, 0, nullptr, nullptr);
      return ptr;
   }
   while(true){
      if(ptr->Key>keyIn){
         if(ptr->left==nullptr){
            ptr->left = new Node(keyIn, 0, nullptr, nullptr);
            return ptr->left;
         }
         else{
            ptr = ptr->left;
            continue;
         }
      }
      else{
         if(ptr->right==nullptr){
            ptr->right = new Node(keyIn, 0, nullptr, nullptr);
            return ptr->right;
         }
         else{
            ptr = ptr->right;
            continue;
         }
      }
   }
}



template<typename K, typename V>
typename Map<K,V>::Node* Map<K,V>::find(Node* temp, const K& keyIn ) const
{
   Node* ptr = temp;
   while(true){
      if(ptr==nullptr){
         return ptr;
      }
      if(ptr->Key==keyIn){
         return ptr;
      }
      if(ptr->Key>keyIn){
         ptr = ptr->left;
         continue;
      }
      else{
         ptr = ptr->right;
         continue;
      }
   }

}



//Traverse function & helper                                                                                                                                               
//                                                                                                                                                                         
template<typename K, typename V>
void Map<K,V>::traverseHelp(void (*f)(const K& keyIn, const V& valIn), Map<K,V>::Node* root){
   cout << "got here 2 " << endl;
   if(root->left==nullptr && root->right==nullptr){
      cout << "Apply F" << endl;
      f(root->Key, root->Value);
   }
   else if(root->left!=nullptr){
      cout << "Traverse left" << endl;
      traverseHelp(*f, root->left);
   }
   else if(root->right!=nullptr){
      cout << "Traverse right" << endl;
      traverseHelp(*f, root->right);
   }
   cout << "Got here 3" << endl;
}


template<typename K, typename V>
void Map<K,V>::traverse(void (*f)(const K& keyIn, const V& valIn)){
   cout << "got here" <<endl;
   traverseHelp(*f, head);
}

freq.cc

#include<iostream>
#include<iomanip>
#include<algorithm>
#include"Map.h"
#include<string>
using namespace std;

void printMap(const string&, const int&);


int main() {
   string input;
   Map<string, int> words;

   //Input for map values                                                                                                                                                  
   //                                                                                                                                                                      
   cout << "Enter words seperated by a new line. Type -1 to end" << endl;
   cin >> input;
   while(input != "-1") {
      transform(begin(input), end(input), begin(input), ::tolower);
      words[input]++;
      cout << "Value is now " << words[input] << endl;
      cin >> input;
   }

   //First list sorted alphabetically                                                                                                                                      
   //                                                                                                                                                                      
   cout << "\n" << setw(17) << left << "Word" << setw(1) << setw(10) << right << "Frequency" << endl;
   cout << setfill('-') << setw(17) << right << " " << setfill(' ') << setw(1) << setfill('-') << setw(10) << left << " " << setfill(' ') << endl;

   words.traverse(printMap);
   cout << "Traverse completed" << endl;

   return 0;
}


//Function that prints the key and value of a node                                                                                                                         
//                                                                                                                                                                         
void printMap(const string& key, const int& val){
   cout << setw(17) << left << key << setw(1) << setw(10) << right << val << endl;
}

The code is expected to output a list of keys sorted by frequency, but it gets to the line "got here 2", then returns a segmentation fault.

Likely, root is a nullptr when you get the segmentation fault.

I might be missing something, but you set head to nullptr in the constructor, I don't see where it's ever changed.

I assume you think you set it in insert at some point, but you don't.

Even with that fixed --- if you don't add anything to the map, it will crash, too, because in deleteNotes you never check for a nullptr, but you call it with head as argument from the destructor (which will be nullptr).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM