0

I am trying to create a trie, but when I initialize the pointers in the array to NULL it breaks the program. The program finishes but won't output anything. Why is it doing this I look at online examples and they are doing it.

class trie
    {
    private:
        struct Node
        {
            char letter;
            Node *children[26];

        };

        //the beginning of the trie
        Node *root;

    public:
        /* Constructors with No Arguments */
        trie(void);

        /* Destructor */
        ~trie(void);

        //Function to insert string into the trie.
        void insert(string word);
        //Function to help insert
        void insertHelper(string word, Node * & trieNode);
        //Funtion to print the contents of the trie.
        void printTrie();
        //Function to get the index if a char matches.
        int getIndex(char letter);
    };
    trie::trie()
    {
        /* Initialize the root of the node */
        root = NULL;
        for(int i = 0; i < 26; i++){
        root->children[i] = NULL;
        }
    }
6
  • 1
    Where is the code that runs? Commented Oct 8, 2018 at 12:48
  • 2
    Welcome to Stack Overflow! It sounds like you may need to learn how to use a debugger to step through your code. With a good debugger, you can execute your program line by line and see where it is deviating from what you expect. This is an essential tool if you are going to do any programming. Further reading: How to debug small programs Commented Oct 8, 2018 at 12:51
  • 1
    You seem to be aware of what you're doing, dereferencing a null pointer. Haven't your teacher, book or tutorial told you that you can't dereference a null pointer? Attempting to do so leads to undefined behavior and possible crashes. Commented Oct 8, 2018 at 12:51
  • 4
    In trie::trie(): root = NULL; and two lines later root->children. This is accessing a null pointer! Commented Oct 8, 2018 at 12:52
  • root->children[i] = NULL; -- Problem is -- there is no root. Read your code carefully, as the issue is just two lines above the line in my comment. -- Why is it doing this I look at online examples -- That may explain the issue. You're looking at "online examples" and blind copying instead of actually learning what the code is doing. Commented Oct 8, 2018 at 12:53

1 Answer 1

3
trie::trie()
{
    root = NULL;
    for(int i = 0; i < 26; i++){
    root->children[i] = NULL;  // you are following the nullptr
    }
}

In modern C++ you should use nullptr instead of NULL. No, in fact you should be using smart pointers like std::shared_ptr<> and std::unique_ptr<> or std::vector<>.

I suggest you read #2 of the Ten Commandments for C Programmers :

2: Thou shalt not follow the NULL pointer, for chaos and madness await thee at its end.

Clearly the holy scriptures were mis-transcribed here, as the words should have been ``null pointer'', to minimize confusion between the concept of null pointers and the macro NULL (of which more anon). Otherwise, the meaning is plain. A null pointer points to regions filled with dragons, demons, core dumps, and numberless other foul creatures, all of which delight in frolicing in thy program if thou disturb their sleep. A null pointer doth not point to a 0 of any type, despite some blasphemous old code which impiously assumes this.

"Following the NULL pointer" here means to dereference it.

Sign up to request clarification or add additional context in comments.

7 Comments

I don't think that's his problem . Look at @Scheff comment on the question.
@sagi It is deferentially an issue. It's UB and it's not setting the children of the correct node to null.
I do think that's his problem. Look at @Scheff comment on the question.
I remember that on SGI Irix, accessing a NULL pointer didn't crash immediately as I was used on x86 before. What it actually did I did never find out. (Although, the S/W always had some mysterious bugs I never found.) Porting this code to Linux, we didn't even reach main() and realized that we had done some things very wrong... ;-)
@Swordfish I only realized in the comments that you are meaning to say "a nullpointer is dereferenced here, this is UB". At least to me, "following a pointer" does not unambiguously mean "dereference a pointer". Since your only commentary outside the code comment (and commandment) talks about using nullptr instead of NULL and similar code quality things, it's not actually obvious what you consider the main problem.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.