打印同一个数组,在不同的函数中是不同的,为什么呢?

曾克兰

我正在编写一个程序来解决电路布线中两点之间的最短路径。我用的BFS(breadth search first)方法。我把路径保存在路径数组中,但发现路径最后一个点的坐标不是我想要的(有时值正确有时不正确)。于是,我在findPath函数中打印出了路径数组,这是正确的,但在主函数中不正确。为什么?

我的描述有什么问题?我的母语不是英语,我的英语不是特别好。如果我说的不清楚,请指出。英文检索能力还是很差,不知道这个问题有没有重复,如有重复请见谅!非常感谢你!

#include<iostream>
#include <iomanip>
#include<queue> 
using namespace std;

const int M = 9;
const int MAXLEN = 30;

class Point
{
public:
    int x;  
    int y;
};
//The increments in the x and y direction,
// in the order of up, right, down, and left
int dx[4] = {0,1,0,-1};   
int dy[4] = {-1,0,1,0};

void findPath(int G[][M],Point start,Point finish,int& pathLen,Point path[])
{
    //Find the shortest path from start to finish and its length, pathLen
    if(start.x == finish.x && start.y == finish.y)     //Two overlapping
    {
        pathLen = 0;
        return ; 
    } 
    Point cur,next;
    G[start.x][start.y] = 1;                //Start of blockade
    queue<int> qx,qy;                       //Coordinates the queue 
    qx.push(start.x);    qy.push(start.y);  //The starting point enters the queue 
    while(!qx.empty())
    {
        cur.x = qx.front(); qx.pop();      // 'cur' stands for current position
        cur.y = qy.front(); qy.pop();
        for(int i = 0;i < 4; i++)
        {
            next.x = cur.x + dx[i];  next.y = cur.y + dy[i];   // next is the next position 
            if(!G[next.x][next.y])              //The location is not marked 
            {
                G[next.x][next.y] = G[cur.x][cur.y] + 1;            
                if(next.x == finish.x && next.y == finish.y)    //Finish line 
                {
                        break;
                }   
                qx.push(next.x);
                qy.push(next.y); 
            } 
        }
        if(next.x == finish.x && next.y == finish.y) break;   //Finish line 
    } 

    //Structural path 
    pathLen = G[finish.x][finish.y]; 
    cur = finish;
    for(int i = pathLen-1; i >= 0; i--)
    {
        path[i] = cur;      //Record current position
        for(int j = 0; j < 4; j++)          //Look for the previous position 
        {
            next.x = cur.x + dx[j];         
            next.y = cur.y + dy[j];
            if(G[next.x][next.y] > -1 && G[next.x][next.y] < G[cur.x][cur.y]) 
            {
                break;
            } 
        }
        cout<<"path["<<i<<"]="<<path[i].x<<","<<path[i].y<<endl;
        cur = next;  //Move to current position
    } 
} 

int main()
{
    int G[M][M];          //Grid map
    Point start,finish;   // The starting point and end point 
    int pathLen;          //Shortest path length 
    for(int i = 0; i < M; i++)  //Initializes the outer fence as -1 
    {
        G[0][i] = G[M-1][i] = G[i][0] = G[i][M-1] = -1; 
    }
    for(int i = 1; i < M-1; i++)    //Initialize the region in the grid to 0
    {
        for(int j = 1; j < M-1; j++)
            G[i][j] = 0; 
    }

    G[5][1] = -1; G[6][1] = -1; G[6][2] = -1; G[6][3] = -1; G[7][1] = -1;
    G[7][2] = -1; G[7][3] = -1; G[1][3] = -1; G[2][3] = -1; G[2][4] = -1;
    G[3][5] = -1; G[4][4] = -1; G[4][5] = -1; G[5][5] = -1;     // Inside a wall
    for(int i = 0; i < M; i++)   //Initial time map
    {
        for(int j = 0; j < M; j++)
        {
            cout<<setw(4)<<G[i][j]; 
        }
        cout<<endl;
    }
    start.x = 3; start.y = 2; finish.x = 4; finish.y = 6;
    Point *path = new Point[M]; 
    findPath(G,start,finish,pathLen,path);
    for(int i = 0; i < M; i++)   //Complete time map
    {
        for(int j = 0; j < M; j++)
        {
            cout<<setw(4)<<G[i][j]; 
        }
        cout<<endl;
    }
    for(int i = 0; i < pathLen; i++)
    {
        cout<<"path["<<i<<"]="<<path[i].x<<","<<path[i].y<<endl;
    }
    return 0;
} 

output:如下图所示,函数中path[9]输出与findPath函数中输出不同main在此处输入图片说明

有时结果是对的

在此处输入图片说明

氧化锌

Valgrind 说:

==27791== Invalid read of size 4
==27791==    at 0x401B7B: main (delme.cpp:113)
==27791==  Address 0x4daf108 is 0 bytes after a block of size 72 alloc'd
==27791==    at 0x4839593: operator new[](unsigned long) (vg_replace_malloc.c:433)
==27791==    by 0x401A03: main (delme.cpp:101)
==27791== 
==27791== Invalid read of size 4
==27791==    at 0x401BA6: main (delme.cpp:113)
==27791==  Address 0x4daf10c is 4 bytes after a block of size 72 alloc'd
==27791==    at 0x4839593: operator new[](unsigned long) (vg_replace_malloc.c:433)
==27791==    by 0x401A03: main (delme.cpp:101)

您正在访问错误的字节(在分配的内存之外)

valgrind 报告的行是

    cout<<"path["<<i<<"]="<<path[i].x<<","<<path[i].y<<endl;

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

为什么同一个指针的地址不同?

为什么变量不是在同一个函数中定义的,而是在不同的 if/else 语句中定义的?

为什么我从同一个查询中得到两个不同的结果

R:不同的函数报告同一个数据帧的不同大小

为什么我的 C 代码在引用同一个变量时会打印出不同的值?

为什么两个绑定到同一个函数会返回不同的值

为什么这个 javascript 函数为同一个查询返回两个不同的结果?

同一个函数中的同一个类不同的id,以避免额外的代码

为什么同一个数组不相等?

Lisp:同一个数学函数在不同时间计算出不同的值?

为什么类模板中的成员函数在同一个地址创建同一个对象

如何使用Mongodb将同一个名称的文档归为一个数组中的特定属性具有不同值的同名文档?

为什么numpy从同一个随机状态返回不同的随机数?

为什么同一个程序在Windows上与Mac上运行的方式不同

为什么同一个地址返回不同的值?

为什么从同一个包创建不同的对象

为什么elasticsearch 对不同索引的同一个查询返回的结果差别太大?

为什么同一个ajax调用有不同的请求头?

为什么运行同一个程序需要不同的时间?

为什么同一个磁盘读取测试结果如此不同

为什么我从同一个神经网络模型得到不同的预测?

为什么同一个事件识别不同的activeElements?

在同一个 ng-repeat 中迭代两个不同的数组

为什么同一个笔记本在两个不同的环境中分配大量不同的 vram?

同一个变量中的不同值

检测同一个id中的不同值

在同一个表中插入不同的值

在同一个按钮中添加不同的图像

同一个numpy数组中的不同十进制格式