-1
struct Node{
    Node* ch[26];
    string str;
    Node(){
        for(int i = 0; i < 26; i++) {
            ch[i] = NULL;
        }
        str = "";
    }
};
class Solution {
public:
    vector<string> results;
    Node* root;
    void insert(Node* p, string s) {
        int len = s.size();
        for(int i = 0; i < len; i ++) {
            if(p->ch[s[i] - 'a'] == NULL) {
                p->ch[s[i] - 'a'] = new Node();
            }
            p = p->ch[s[i] - 'a'];
        }
        p->str = s;
    }
   vector<string> wordSearchII(vector<vector<char> > &board, vector<string> &words) {}
}

This is the Trie I defined for my problem. The "root" and "vector result" are both member variables of Solution. The question I want to ask is that why I must "new Node()" before I use root. I do not need to "new vector" before I use results. I understand that the Solution will call default constructor and then "results" will call its default constructor. Why couldn't root use the default constructor of Node?

I happen to realize that my confuse may relate to "pointer". But I still don't understand the details. Can anyone explain about it? I really appreciate it.

  • I dont understand your question. – amanuel2 Oct 20 '16 at 01:14
  • @amanuel2 I try to use root directly in function "wordSearchII". It does not work. If I use "root = new Node()" before I insert the words into the Trie, it works. I just think the Solution class will initialize "Node* root" for me. Another way to express my question is: will pointer like root be default initialized? – wangzishan Oct 20 '16 at 01:46
  • `Solution` will not initialize `root` to anything, because you are not telling it to! NO, a pointer is not default-initialized, you have to initialize it yourself. – Remy Lebeau Oct 20 '16 at 02:21

2 Answers2

0

In

Node* root;

The * means that root is a pointer to a Node object, it is not an actual Node object itself. The only storage a pointer has is enough to hold the memory address of whatever the pointer is pointing at. A pointer has to point at something. Right now, you have no idea what root points at, and this is... bad. You have no idea what you'll get if you try to read from it, and you have no idea what you'll smash if you try to write to it. You need to assign something for root to point at, and if that's a new Node(), so be it, but it could also be a pre-existing Node, or a Node in automatic storage (AKA somewhere on the stack).

On the other hand, in

vector<string> results;

results is not a pointer to a vector object. It is the actual vector object. It is the storage. There is no need to allocate memory for it, simply declaring it on the stack allocates everything for it and calls its default constructor.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user4581301
  • 33,082
  • 7
  • 33
  • 54
0

root is just a pointer, but you are not assigning anything for it to point at. You need to allocate a new Node object and then assign the address of that object to root, eg:

class Solution {
public:
    vector<string> results;
    Node* root;

    Solution() {
        root = new Node;
    }

    ~Solution() {
        delete root;
    }

    ...
};

Otherwise, don't make root be a pointer at all:

class Solution {
public:
    vector<string> results;
    Node root;

    ...
};

On a side note, your Node class needs a destructor to destroy any child nodes that are added to it:

struct Node{
    Node* ch[26];
    string str;

    Node(){
        for(int i = 0; i < 26; i++) {
            ch[i] = NULL;
        }
    }

    ~Node(){
        for(int i = 0; i < 26; i++) {
            delete ch[i];
        }
    }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks for your answer. I have only one small question. For the first piece of code, you use "root = new Node;". I used to add parentheses after this initialization. For example, "root = new Node();". As I have checked the way you initialize. You are also right. Are these two ways both the "default constructor"? – wangzishan Oct 20 '16 at 03:15
  • @wangzishan see [Do the parentheses after the type name make a difference with new?](http://stackoverflow.com/questions/620137/) – Remy Lebeau Oct 20 '16 at 15:38