-2

I keep getting this error. I know what function causes it, but don't know how to fix it. Looking up online from this post saying:

You need to pass a pointer to a dynamically allocated object, or make your own insde your chainLink class.

However, as I try to pass a string pointer. error still popping up. Here is my code.

#include <iostream>
#include "MWTNode.h"
#include "MWT.h"
using namespace std;

int main() {
MWT t;
string str ="abc";
string* strPtr = &str;
t.insert(strPtr);
std::cout << "Hello, World!" << std::endl;
return 0;
}


#include "MWTNode.h"
class MWT {
public:
MWTNode *root;
string find(const string &);
void insert(const string* string);

};
void MWT::insert(const string* word) {
MWTNode* curr = root;
MWTNode newNode;
string w = *word;
for (int i = 0; i < word->length(); i++) {
    const char c = w[i];
    if (curr->children.find(c) == curr->children.end()){

        //curr->children[c]= MWTNode();
        //node->frequency=node->frequency+1;
    }
    curr = &(curr->children[c]);
}
curr->flag = true;
}



#include <unordered_map>
#include <vector>
#include <string>
#include <sstream>
#include <set>
using namespace std;

class MWTNode {
public:
unordered_map<char, MWTNode> children;
string value;
bool flag;
int frequency;

MWTNode(const string &);
MWTNode(const char c);
MWTNode();
void setFrequency ();
int getFrequency ();

};


MWTNode::MWTNode(const string &val) {
value = val;
flag = false;
frequency = 0;
}

MWTNode::MWTNode(const char c) {
value =c;
flag = false;
frequency = 0;
}

MWTNode::MWTNode() {
value ="";
flag = false;
frequency = 0;
}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 1
    The pointer is useless, as you copy the string it points to and never use the pointer again. – Some programmer dude Feb 19 '18 at 07:06
  • After trimming the chatty material, your title is "C++ pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug". Is this an error message? Where is it from? – halfer Feb 19 '18 at 17:06

1 Answers1

1

Lets highlight a few lines of the code you show

class MWT {
public:
MWTNode *root;
// ...
};

In that you declare the member variable root as a pointer.

void MWT::insert(const string* word) {
MWTNode* curr = root;
// ...
}

In the above you make curr point to where root is pointing.

But you never make root point anywhere! The MWT::root variable is uninitialized and will have an indeterminate value. Using this pointer in any way without initialization will lead to undefined behavior.

And yes you use this pointer, as you dereference curr inside the MWT::insert function.

It's a little unclear what you're doing (to me) but you need to make sure that root (and therefore curr) is a valid pointer before attempting to dereference it.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I am trying to implement multiway trie using unordered_map, so I created a MWTNode class and MWT class. then trying to create a MWT by calling its inert? which part is not clear, please let me know if you would suggest a diff way to do this? –  Feb 19 '18 at 19:42