Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8074103
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:32:51+00:00 2026-06-05T14:32:51+00:00

I encountered a weird bug I am returning a pointer, before return, I verified

  • 0

I encountered a weird bug

I am returning a pointer, before return, I verified that the pointer is valid & has memory
However, after the function scope, when I tried to use the return value in main(), it becomes NULL.
I also tried to return the dereferenced value of the pointer, it is a modified struct before return, and an unmodified struct in main()..

This is supposed to be like a dictionary

#include <iostream>
#include <fstream>
#include <string>
#include "trie.h"

using namespace std;

int alphaLoc(char segment){
    return (int)segment - 97;
}

//inserts a word in the tree
void insert(TrieNode &node, const std::string &word){
    int locationInAlphabet = alphaLoc(word[0]);
    if (node.letters[locationInAlphabet] == NULL){
        node.letters[locationInAlphabet] = new TrieNode;
    }
    if (word.length() == 1){
        if (node.letters[locationInAlphabet]->isWord == true){
            cout<<"Word Already Exsit"<<endl;
        }
        node.letters[locationInAlphabet]->isWord = true;
    }
    else{
        insert(*(node.letters[locationInAlphabet]), word.substr(1,word.length()-1));
    }
}

//returns the node that represents the end of the word
TrieNode* getNode(const TrieNode &node, const std::string &word){
    int locationInAlphabet = alphaLoc(word[0]);
    if (node.letters[locationInAlphabet] == NULL){
        return NULL;
    }
    else{
        if (word.length() == 1){
            return (node.letters[locationInAlphabet]);
        }
        else{
            getNode(*(node.letters[locationInAlphabet]), word.substr(1,word.length()-1));
        } 
    }
}

int main(){
    TrieNode testTrie;
    insert(testTrie, "abc");
    cout<< testTrie.letters[0]->letters[1]->letters[2]->isWord<<endl;
    cout<<"testing output"<<endl; 
    cout<< getNode(testTrie, "abc")->isWord << endl;
    return 1;
}

And the output is :

1
testing output
Segmentation fault: 11

trie.h:

#include <string>

struct TrieNode {
    enum { Apostrophe = 26, NumChars = 27 };
    bool isWord;
    TrieNode *letters[NumChars];
    TrieNode() {
        isWord = false;
         for ( int i = 0; i < NumChars; i += 1 ) {
             letters[i] = NULL;
         } // for
    }
}; // TrieNode

void insert( TrieNode &node, const std::string &word );

void remove( TrieNode &node, const std::string &word );

std::string find( const TrieNode &node, const std::string &word );
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T14:32:53+00:00Added an answer on June 5, 2026 at 2:32 pm

    You have return missing before getNode(*(node....

    If this line is executed in some moment, after this execution control flow reaches the end of getNode function, and no return statement is here. It will result in undefined return value, which is always bad. You must always return something definite from your function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just encountered a weird bug/feature in table cells under Chrome & Firefox. it
I've encountered a weird issue that has gotten me stumped for a few days
I just encountered a weird error which saying that find is not a member
I have this weird situation that i have encountered for the first time and
I'm encountered something weird, and I'm not sure if it's a bug in Visual
I encountered some very weird behaviour which I think is a bug, but I
I encountered following weird behavior yesterday. It seems a compiler bug to me or
Here is the script that i wrote and it has weird syntax error at
I encountered a very weird bug where when I try to click and drag
I had an if statement checking some value, And encountered a weird bug(Not sure!).

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.