C++内存分配失败(使用 new & delete)



我是一个普通学生,试图编码蒙特卡洛树搜索。

为此,我制作了一个名为" node"的结构,并试图用"新"来制作新的节点*指针并分配内存,但是我的程序不断崩溃,我会再次感谢您的帮助。

这是我的代码的样子。故障位置被标记。

预先感谢您。

Node* knell;
Node* treePolicy(Node* currentnode){
    puts("treepolicy");
    Node* temp=(Node*)malloc(sizeof(Node));
    puts("Mem Allocated");
    Move save=MCTBoard.generateRandomLegalMove(currentnode->player);      ///works very well up to here.
    save.printMove();
    temp=findNode(currentnode,save);
    puts("Node Found");
    knell=new Node; 
    if(temp==NULL){
        free(temp);
        temp = new Node; ///crashes the second time treePolicy(Node*) is called.
        temp->setMove(save);
        temp->child.clear();
        currentnode->child.push_back(temp);
        temp->parent=currentnode;
        MCTBoard.playMove(save,currentnode->player);
        for(int i=0;i<4;i++){
            for(int j=0;j<=i;j++){
                for(int k=0;k<=i;k++){
                    temp->board[i][j][k]=MCTBoard.a[i][j][k];
                }
            }
        }
        temp->value=temp->visited=temp->win=temp->draw=0;
        temp->player=3-currentnode->player;
        knell=temp;
        //delete temp; -> even with this enabled, still crashes.
        return knell;///an infinite loop happens here, but that is another problem so...
    }
else{
    ///not important,and besides,I've not even reached here yet once.
}

}

实际上在另一个功能中确实存在相同的问题,但是我觉得那是其他问题。

所以,任何人都可以告诉我为什么会崩溃?

让我们走过temp的生命周期。

您使用malloc分配了内存。

Node* temp=(Node*)malloc(sizeof(Node));

,然后您通过以下功能的返回值覆盖temp

temp = findNode(currentnode,save);

然后您使用free删除它。

free(temp);

使用free释放的内存从findNode返回。崩溃的原因很可能是该函数返回的内存使用new而不是malloc

几个评论:

Node* knell;

为什么要使knell成为全局变量?为什么不处理函数外部全局的分配(也许看起来像:

knell = treePolicy(currentNode);

)或类似的东西

Node* treePolicy(Node* currentnode){
    puts("treepolicy");
    Node* temp=(Node*)malloc(sizeof(Node));

没有理由在这里进行Malloc。您将稍后覆盖,所以让我们删除此行。

    puts("Mem Allocated");
    Move save=MCTBoard.generateRandomLegalMove(currentnode->player);      ///works very well up to here.
    save.printMove();
    temp=findNode(currentnode,save);

这是自动的好地方。

    puts("Node Found");
    knell=new Node; 

就像我们上面说的那样,删除此。

    if(temp==NULL){

nullptr的好地方

        free(temp);

我们刚刚确定temp为无效。无需免费

        temp = new Node; ///crashes the second time treePolicy(Node*) is called.
        temp->setMove(save);
        temp->child.clear();
        currentnode->child.push_back(temp);
        temp->parent=currentnode;
        MCTBoard.playMove(save,currentnode->player);
        for(int i=0;i<4;i++){
            for(int j=0;j<=i;j++){
                for(int k=0;k<=i;k++){
                    temp->board[i][j][k]=MCTBoard.a[i][j][k];
                }
            }
        }
        temp->value=temp->visited=temp->win=temp->draw=0;
        temp->player=3-currentnode->player;
        knell=temp;

让我们摆脱这个

        //delete temp; -> even with this enabled, still crashes.
        return knell;///an infinite loop happens here, but that is another problem so...

好吧,所以现在没有理由返回knell,而只需返回temp

    }
else{
    ///not important,and besides,I've not even reached here yet once.
}

因此,固定的UP代码应该看起来像:

   Node* treePolicy(Node* currentnode){
        puts("treepolicy");
        Move save=MCTBoard.generateRandomLegalMove(currentnode->player);      ///works very well up to here.
        save.printMove();
        auto temp=findNode(currentnode,save);
        puts("Node Found");
        if(temp==nullptr){
            temp = new Node; ///crashes the second time treePolicy(Node*) is called.
            temp->setMove(save);
            temp->child.clear();
            currentnode->child.push_back(temp);
            temp->parent=currentnode;
            MCTBoard.playMove(save,currentnode->player);
            for(int i=0;i<4;i++){
                for(int j=0;j<=i;j++){
                    for(int k=0;k<=i;k++){
                        temp->board[i][j][k]=MCTBoard.a[i][j][k];
                    }
                }
            }
            temp->value=temp->visited=temp->win=temp->draw=0;
            temp->player=3-currentnode->player;
            return temp;
        }
    else{
        ///not important,and besides,I've not even reached here yet once.
    }

或某种效果。

最新更新