简体   繁体   English

C ++函数返回的值未分配

[英]C++ function returned value not assigned

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. 我返回一个指针,在返回之前,我验证指针是有效的并且有内存但是,在函数作用域之后,当我尝试使用main()中的返回值时,它变为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().. 我还尝试返回指针的解除引用值,返回前是修改后的结构,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: 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 );

You have return missing before getNode(*(node... . 你在getNode(*(node...之前return缺失。

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. 如果此行在某个时刻执行,则此执行控制流到达getNode函数的末尾,此处没有return语句。 It will result in undefined return value, which is always bad. 它将导致未定义的返回值,这总是很糟糕。 You must always return something definite from your function. 你必须总是从你的功能中返回确定的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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