【问题标题】:Memory Access Violation Insert Struct Within Tree Struct C++内存访问冲突在树结构 C++ 中插入结构
【发布时间】:2017-03-23 20:44:38
【问题描述】:

我正在尝试创建一个结构树并将树节点的数据插入到包含两个数据持有者的结构中。我的树/数据结构如下所示:

class BinarySearchTree
{
private:

struct IndexEntry
{
    int acctID;   // (key) Account identifier
    long recNum;  // Record number
};

struct tree_node
{
    IndexEntry* entry;
    tree_node* left;
    tree_node* right;
};
tree_node* root;

public:
BinarySearchTree()
{
    root = NULL;
}

bool isEmpty() const { return root == NULL; }
void insert(int, int);
int search(int);
int treeSearch(tree_node*, int);
};

此时我在插入函数中遇到内存访问冲突,老实说,这是我第一次尝试结构树,所以我不知道它是否是正确的插入开始的功能。但这里是:

void BinarySearchTree::insert(int rNum, int aNum)
{
tree_node* t = new tree_node;
tree_node* parent;
t -> entry -> recNum = rNum; //right here I get a violation
t -> entry -> acctID = aNum; //but if I remove the assignments
t -> left = NULL;            //it gives me a violation further down
t -> right = NULL;
parent = NULL;

if (isEmpty())
    root = t;
else
{
    tree_node* current;
    current = root;
    // Find the Node's parent
    while (current)
    {
        parent = current; //This whole block will give me a memory violation
        if (t -> entry -> recNum > current -> entry -> recNum) 
            current = current -> right;
        else current = current -> left;
    }

    if (t -> entry -> recNum < parent -> entry -> recNum)
        parent -> left = t;
    else
        parent -> right = t;
}
}

有关内存访问违规的位置,请参阅我的第二个代码块中的 cmets。我在想代码中有一些未初始化的东西,但我真的不知道它会在哪里或如何初始化它。

任何帮助或指导将不胜感激!

【问题讨论】:

  • 你从未初始化过t-&gt;entry
  • 不要在-&gt; 周围加空格,这不习惯。
  • 特别是不要和&gt;操作符混用。看起来像一列箭。

标签: c++ data-structures struct binary-search-tree


【解决方案1】:

你需要初始化t-&gt;entry

tree_node *t = new tree_node;
t->entry = new IndexEntry;

【讨论】:

    【解决方案2】:

    您正在取消引用未初始化的指针。当你这样做时:

    tree_node* t = new tree_node;
    

    然后编译器将执行实际上什么都不做的默认构造函数。 t-&gt;entry 未分配任何值并包含垃圾。

    所以稍后当你取消引用它时:

    t -> entry -> recNum = rNum; //right here I get a violation
    

    t -&gt; entry -&gt; 是取消引用操作),您将获得未定义的行为,这在您的情况下会导致崩溃。

    解决方案是在取消引用之前初始化t -&gt; entry

    【讨论】:

      【解决方案3】:

      tree_node 中的entry 指针未正确初始化,它是一个指针,它没有指向有效对象。可以在构造函数中初始化,在析构函数中不要忘记删除。

      struct tree_node 
      {
          IndexEntry *entry;
          tree_node *left;
          tree_node *right;
      
          tree_node() :
              entry(new IndexEntry), // create a new entry object
              left(NULL), right(NULL)
          {}
      
          ~tree_node() 
          {
              delete entry; // release the memory when we're done
          }
      };
      

      事实上,我不明白为什么你首先需要在堆中创建IndexEntry。似乎entrytree_node 的一部分,所以您可以简单地将其“嵌入”到tree_node

      struct tree_node
      {
          IndexEntry entry; // not a pointer, but an object
          tree_node *left;
          tree_node *right;
      };
      

      当然在访问entry的成员时需要使用.

      tree_node *t = new tree_node;
      t->entry.recNum = rNum;
      t->entry.acctID = aNum;
      t->left = NULL;
      t->right = NULL;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多