在C+中实现*时可能出现内存泄漏+; 我已经编程了一段时间,但我对C++有了新的认识。我正在尝试实现A*算法,并已成功生成以下代码。该实现产生了预期的结果,即2D网格中从A点到B点的最短路径,但我怀疑我留下了一些内存泄漏

在C+中实现*时可能出现内存泄漏+; 我已经编程了一段时间,但我对C++有了新的认识。我正在尝试实现A*算法,并已成功生成以下代码。该实现产生了预期的结果,即2D网格中从A点到B点的最短路径,但我怀疑我留下了一些内存泄漏,c++,debugging,memory-leaks,a-star,C++,Debugging,Memory Leaks,A Star,我通过重载new和delete操作符来测试这一点,以跟踪在堆上分配的字节数,这表明很多memeory从未被释放。然而,我也测试了从未被删除的节点的数量,但它表明,所有分配的节点都调用了它们的析构函数。请注意,在代码中,我只在节点上调用new,因此产生了混淆。我对此有很多困惑,希望能解释一下 我曾尝试使用智能指针,但最终使用了难以解析的循环引用 这也是我关于堆栈溢出的第一篇文章,所以请随意指出我可以如何改进我的问题。提前谢谢 #include<vector> #include<a

我通过重载new和delete操作符来测试这一点,以跟踪在堆上分配的字节数,这表明很多memeory从未被释放。然而,我也测试了从未被删除的节点的数量,但它表明,所有分配的节点都调用了它们的析构函数。请注意,在代码中,我只在节点上调用new,因此产生了混淆。我对此有很多困惑,希望能解释一下

我曾尝试使用智能指针,但最终使用了难以解析的循环引用

这也是我关于堆栈溢出的第一篇文章,所以请随意指出我可以如何改进我的问题。提前谢谢

#include<vector>
#include<array>
#include<cmath>

int mynodes = 0;
int memory_left = 0;

void* operator new(size_t size)
{
    memory_left += size;
    return malloc(size);
}

void operator delete(void* memory, size_t size)
{
    memory_left -= size;
    free(memory);
}

struct Node{
    std::array<int, 2> position;
    Node* parent = nullptr;
    double h, g, f;

    Node(const std::array<int, 2>& pos)
        :position(pos){mynodes++;}


    ~Node(){ mynodes--; }
};


std::vector<std::array<int, 2>> find_children(const std::vector<std::vector<int>>& grid, const std::array<int, 2>& pos){

    std::vector<std::array<int, 2>> children;
    children.reserve(8);

    for(int t = -1; t < 2; t++){
        for(int q = -1; q < 2; q++){
            if(t != 0 || q != 0){
                if(abs(t) == abs(q)) continue;

                std::array<int, 2> cur_pos = {pos[0]+q, pos[1]+t};
                if(cur_pos[0] >= 0 && cur_pos[0] < grid[0].size() && cur_pos[1] >= 0 && cur_pos[1] < grid.size())
                {
                    if(grid[cur_pos[1]][cur_pos[0]] == 0)
                    {
                        children.push_back(cur_pos);
                    }
                }
            }
        }
    }

    return children;
}

bool search_vect(const std::vector<Node*>& set, const std::array<int, 2>& pos)
{
    for(Node* node : set)
    {
        if(node->position[0] == pos[0] && node->position[1] == pos[1]) return true;
    }
    return false;
}

void releaseNodes(std::vector<Node*>& set)
{
    for(auto& node : set)
    {
        delete node;
    }
    set.clear();
}

std::vector<std::array<int, 2>> find_path(const std::vector<std::vector<int>>& grid, std::array<int, 2> start, std::array<int, 2> end){

    Node* cur_node = new Node(start);
    std::vector<Node*> open_vect;
    std::vector<Node*> closed_vect;
    open_vect.push_back(cur_node);

    while(cur_node->position != end){

        double lowest_f = INFINITY;
        size_t idx = 0;
        for(size_t i = 0; i < open_vect.size(); i++)
        {
            if(open_vect[i]->f < lowest_f)
            {
                cur_node = open_vect[i];
                lowest_f = cur_node->f;
                idx = i;
            }
        }
        open_vect.erase(open_vect.begin() + idx);

        std::vector<std::array<int, 2>> children = find_children(grid, cur_node->position);
        closed_vect.push_back(cur_node);

        for(const auto& child_pos : children){
            // if(closed_vect.find(child_pos) != closed_vect.end() || open_vect.find(child_pos) != open_vect.end()) continue;
            if(search_vect(closed_vect, child_pos) || search_vect(open_vect, child_pos))
            {
                continue;
            }

            Node* new_node = new Node(child_pos);
            new_node->g = cur_node->g + 1;
            new_node->h = abs(end[0] - child_pos[0]) + abs(end[1] - child_pos[1]);
            new_node->f = new_node->g + new_node->h;
            new_node->parent = cur_node;

            // double h = sqrt(pow(end[0] - child_pos[0], 2) + pow(end[1] - child_pos[1], 2));

            open_vect.push_back(new_node);

        }
    }

    std::vector<std::array<int, 2>> path;
    while(cur_node != nullptr){
        path.push_back(cur_node->position);
        cur_node = cur_node->parent;
    }

    releaseNodes(open_vect);
    releaseNodes(closed_vect);

    return path;
}


int main()
{
    std::vector<std::vector<int>> grid( 100 , std::vector<int> (100, 0));

    {
        auto path = find_path(grid, {1, 1}, {98, 98});
    }


}
#包括
#包括
#包括
int mynodes=0;
int memory_left=0;
void*运算符新(大小\u t大小)
{
内存_左+=大小;
返回malloc(大小);
}
void运算符删除(void*内存,大小\u t大小)
{
内存_left-=大小;
空闲(内存);
}
结构节点{
std::阵列位置;
节点*parent=nullptr;
双h,g,f;
节点(常数std::数组和位置)
:位置(pos){mynodes++;}
~Node(){mynodes--;}
};
std::vector find_子项(const std::vector和grid、const std::array和pos){
性病媒儿童;
儿童保护区(8);
for(int t=-1;t<2;t++){
对于(int q=-1;q<2;q++){
如果(t!=0 | | q!=0){
如果(abs(t)=abs(q))继续;
数组cur_pos={pos[0]+q,pos[1]+t};
如果(cur_pos[0]>=0&&cur_pos[0]=0&&cur_pos[1]位置[0]==位置[0]&节点->位置[1]==位置[1])返回true;
}
返回false;
}
无效释放节点(标准::向量和集)
{
用于(自动&节点:设置)
{
删除节点;
}
set.clear();
}
std::vector find_path(const std::vector&grid,std::array start,std::array end){
节点*当前节点=新节点(开始);
std::向量开放向量;
std::向量闭合向量;
打开向量,向后推(当前节点);
while(当前节点->位置!=结束){
双最低点f=无穷大;
大小\u t idx=0;
对于(size_t i=0;if<最低值)
{
cur_node=open_vect[i];
最低\u f=当前节点->f;
idx=i;
}
}
open_vect.erase(open_vect.begin()+idx);
std::vector children=find_children(网格、cur_节点->位置);
关闭向量推回(当前节点);
用于(const auto&child_pos:children){
//如果(closed_vect.find(child_pos)!=closed_vect.end()| | open_vect.find(child_pos)!=open_vect.end())继续;
if(搜索向量(闭合向量,子位置)|搜索向量(开放向量,子位置))
{
继续;
}
节点*新节点=新节点(子节点);
新建节点->g=当前节点->g+1;
新建节点->h=abs(结束[0]-子节点[0])+abs(结束[1]-子节点[1]);
新建节点->f=新建节点->g+新建节点->h;
新建节点->父节点=当前节点;
//双h=sqrt(pow(end[0]-child_pos[0],2)+pow(end[1]-child_pos[1],2));
打开向量,向后推(新节点);
}
}
向量路径;
while(cur_node!=nullptr){
路径。推回(当前节点->位置);
当前节点=当前节点->父节点;
}
释放节点(打开向量);
释放节点(闭合向量);
返回路径;
}
int main()
{
标准::向量网格(100,标准::向量(100,0));
{
自动路径=查找路径(网格,{1,1},{98,98});
}
}

这里是消除任何显式新建/删除的最小/简单方法:

#包括
#包括
#包括
结构节点{
std::阵列位置;
节点*parent=nullptr;
双h{0};
双g{0};
双f{0};
节点(常数std::数组和位置)
:位置(pos){}
};
std::vector find_子项(const std::vector和grid、const std::array和pos){
性病媒儿童;
儿童保护区(8);
for(int t=-1;t<2;t++){
对于(int q=-1;q<2;q++){
如果(t!=0 | | q!=0){
如果(abs(t)=abs(q))继续;
数组cur_pos={pos[0]+q,pos[1]+t};
如果(cur_pos[0]>=0&&cur_pos[0]=0&&cur_pos[1]位置[0]==位置[0]&节点->位置[1]==位置[1])返回true;
}
返回false;
}
std::vector find_path(const std::vector&grid,std::array start,std::array end){
std::向量节点{};
节点。向后放置(开始);
Node*cur_Node=&nodes.back();
std::向量开放向量;
std::向量闭合向量;
打开向量,向后推(当前节点);
while(当前节点->位置!=结束){
双最低点f=无穷大;
大小\u t idx=0;
对于(size_t i=0;i