打印链接的 C 结构时出现问题

发布于 2024-11-19 22:42:43 字数 3932 浏览 2 评论 0原文

(我的代码中的坐标约定对某些人来说可能相当混乱——这真的很奇怪。除非有人指出这可能是问题所在,否则我不会解释它。我认为问题是我使用了“流动”指针[参见下面的回溯]。)

所以我有这样的结构:

typedef struct node astarnode;
struct node{
    int xpos;
    int ypos;
    astarnode* father;
};

我在其中运行 A* 搜索地图上的路径:

void astar(int m,int n,int sx,int sy,int gx,int gy,char *map[]){
    //Yep. A priority queue. Will post code if this happens to be the problem.
PQueue* nodequeue = (PQueue *) malloc(sizeof(PQueue));
initPQueue(nodequeue);
//Just some global variables
rowlimit = m;
collimit = n;
startx = sx;
starty = sy;
goalx = gx;
goaly = gy;
environment = map;

int row = sx;
int col = sy;
astarnode* thisnode = (astarnode *) malloc(sizeof(astarnode));
thisnode->xpos = col;
thisnode->ypos = row;
//The root of the tree is the node with itself as father.
thisnode->father = thisnode;

while(row != gy || col != gx){
    printf("currently in (%d, %d)\n", row, col);
    printAStarNode(thisnode);
    printf("\n");
    //Tedious move functions---see template below
    //putToQueue won't enqueue astarnodes whose fathers are null---see move functions
    astarnode* upleft = moveUpLeft(col, row, thisnode);
    putToQueue(nodequeue, upleft);
    astarnode* up = moveUp(col, row, thisnode);
    putToQueue(nodequeue, up);
    astarnode* upright = moveUpRight(col, row, thisnode);
    putToQueue(nodequeue, upright);
    astarnode* left = moveLeft(col, row, thisnode);
    putToQueue(nodequeue, left);
    astarnode* right = moveRight(col, row, thisnode);
    putToQueue(nodequeue, right);
    astarnode* downleft = moveDownLeft(col, row, thisnode);
    putToQueue(nodequeue, downleft);
    astarnode* down = moveDown(col, row, thisnode);
    putToQueue(nodequeue, down);
    astarnode* downright = moveDownRight(col, row, thisnode);
    putToQueue(nodequeue, downright);
    //And enqueue
    free(thisnode);
    thisnode = (astarnode *) malloc(sizeof(astarnode));
    thisnode = dequeue(nodequeue, thisnode);
    row = thisnode->ypos;
    col = thisnode->xpos;
}
traceback(thisnode);
}

我的回溯函数是

void traceback(astarnode* asn){
astarnode* rover = asn;

while(rover->xpos != startx || rover->ypos != starty){
    printAStarNode(rover);
    printf("\n");
    rover = rover->father;
}
}

我将 astarnode 打印为:

void printAStarNode(astarnode *asn){
int address = (int) asn->father;
int myaddress = (int) asn;
printf("%d : (%d, %d, %d)", myaddress, asn->ypos, asn->xpos, address);
}

我的移动函数的模板:

astarnode* moveDown(int x, int y, astarnode* pred){
astarnode* downnode = (astarnode *) malloc(sizeof(astarnode));
//Do necessary translations and then reflect to struct
y += 1;
downnode->xpos = x;
downnode->ypos = y;

if(y > rowlimit){ //This move is infeasible.
    downnode->father = NULL;
} else{
    downnode->father = pred;
}

return downnode;
}

为此我得到以下内容跟踪:

currently in (0, 0)
149012584 : (0, 0, 149012584)
currently in (1, 1)
149012712 : (1, 1, 149012584)
currently in (2, 2)
149012888 : (2, 2, 149012712)
currently in (3, 3)
149013080 : (3, 3, 149012888)
currently in (3, 4)
149013240 : (3, 4, 149013080)
currently in (4, 5)
149013512 : (4, 5, 149013240)
currently in (5, 6)
149013720 : (5, 6, 149013512)
currently in (6, 6)
149013880 : (6, 6, 149013720)
currently in (6, 7)
149013912 : (6, 7, 149013720)
currently in (7, 8)
149014392 : (7, 8, 149013912)
currently in (8, 8)
149014616 : (8, 8, 149014392)
<Traceback actually starts here>
149014872 : (9, 9, 149014616)
149014616 : (8, 0, 149014392)
149014392 : (7, 0, 149013912)
149013912 : (6, 0, 149013720)
149013720 : (5, 0, 149013512)
149013512 : (4, 0, 149013240)
149013240 : (3, 0, 149013080)
149013080 : (3, 0, 149012888)
149012888 : (2, 0, 149012712)
149012712 : (1, 0, 149012584)

地址看起来是正确的,但为什么xpos都变成0了?我错过了什么吗?

(The coordinate conventions in my code might be pretty messy for some---it's really weird. I won't explain it unless someone points out that it might be the problem. I think the problem is my use of a "roving" pointer [see traceback below].)

So I have this structure:

typedef struct node astarnode;
struct node{
    int xpos;
    int ypos;
    astarnode* father;
};

on which I run an A* search for a path on a map:

void astar(int m,int n,int sx,int sy,int gx,int gy,char *map[]){
    //Yep. A priority queue. Will post code if this happens to be the problem.
PQueue* nodequeue = (PQueue *) malloc(sizeof(PQueue));
initPQueue(nodequeue);
//Just some global variables
rowlimit = m;
collimit = n;
startx = sx;
starty = sy;
goalx = gx;
goaly = gy;
environment = map;

int row = sx;
int col = sy;
astarnode* thisnode = (astarnode *) malloc(sizeof(astarnode));
thisnode->xpos = col;
thisnode->ypos = row;
//The root of the tree is the node with itself as father.
thisnode->father = thisnode;

while(row != gy || col != gx){
    printf("currently in (%d, %d)\n", row, col);
    printAStarNode(thisnode);
    printf("\n");
    //Tedious move functions---see template below
    //putToQueue won't enqueue astarnodes whose fathers are null---see move functions
    astarnode* upleft = moveUpLeft(col, row, thisnode);
    putToQueue(nodequeue, upleft);
    astarnode* up = moveUp(col, row, thisnode);
    putToQueue(nodequeue, up);
    astarnode* upright = moveUpRight(col, row, thisnode);
    putToQueue(nodequeue, upright);
    astarnode* left = moveLeft(col, row, thisnode);
    putToQueue(nodequeue, left);
    astarnode* right = moveRight(col, row, thisnode);
    putToQueue(nodequeue, right);
    astarnode* downleft = moveDownLeft(col, row, thisnode);
    putToQueue(nodequeue, downleft);
    astarnode* down = moveDown(col, row, thisnode);
    putToQueue(nodequeue, down);
    astarnode* downright = moveDownRight(col, row, thisnode);
    putToQueue(nodequeue, downright);
    //And enqueue
    free(thisnode);
    thisnode = (astarnode *) malloc(sizeof(astarnode));
    thisnode = dequeue(nodequeue, thisnode);
    row = thisnode->ypos;
    col = thisnode->xpos;
}
traceback(thisnode);
}

My traceback function is

void traceback(astarnode* asn){
astarnode* rover = asn;

while(rover->xpos != startx || rover->ypos != starty){
    printAStarNode(rover);
    printf("\n");
    rover = rover->father;
}
}

I print astarnode as:

void printAStarNode(astarnode *asn){
int address = (int) asn->father;
int myaddress = (int) asn;
printf("%d : (%d, %d, %d)", myaddress, asn->ypos, asn->xpos, address);
}

A template of my move functions:

astarnode* moveDown(int x, int y, astarnode* pred){
astarnode* downnode = (astarnode *) malloc(sizeof(astarnode));
//Do necessary translations and then reflect to struct
y += 1;
downnode->xpos = x;
downnode->ypos = y;

if(y > rowlimit){ //This move is infeasible.
    downnode->father = NULL;
} else{
    downnode->father = pred;
}

return downnode;
}

For which I get the following trace:

currently in (0, 0)
149012584 : (0, 0, 149012584)
currently in (1, 1)
149012712 : (1, 1, 149012584)
currently in (2, 2)
149012888 : (2, 2, 149012712)
currently in (3, 3)
149013080 : (3, 3, 149012888)
currently in (3, 4)
149013240 : (3, 4, 149013080)
currently in (4, 5)
149013512 : (4, 5, 149013240)
currently in (5, 6)
149013720 : (5, 6, 149013512)
currently in (6, 6)
149013880 : (6, 6, 149013720)
currently in (6, 7)
149013912 : (6, 7, 149013720)
currently in (7, 8)
149014392 : (7, 8, 149013912)
currently in (8, 8)
149014616 : (8, 8, 149014392)
<Traceback actually starts here>
149014872 : (9, 9, 149014616)
149014616 : (8, 0, 149014392)
149014392 : (7, 0, 149013912)
149013912 : (6, 0, 149013720)
149013720 : (5, 0, 149013512)
149013512 : (4, 0, 149013240)
149013240 : (3, 0, 149013080)
149013080 : (3, 0, 149012888)
149012888 : (2, 0, 149012712)
149012712 : (1, 0, 149012584)

The addresses seem correct but why did the xpos' all become 0? Anything I missed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜灵血窟げ 2024-11-26 22:42:43

我怀疑您的回溯例程打印出您已经释放的节点。 (有关排队的注释后面的 free() 。)

I suspect that your traceback routine prints out nodes that you have already freed. (The free() right after the comment about enqueueing.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文