簡體   English   中英

打印鏈接的 C 結構時出現問題

[英]Problem with printing a linked C structure

(對於某些人來說,我的代碼中的坐標約定可能非常混亂——這真的很奇怪。除非有人指出這可能是問題所在,否則我不會解釋它。我認為問題在於我使用了“漫游”指針[參見下面的回溯]。)

所以我有這個結構:

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

我在其中運行 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);
}

我的回溯 function 是

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? 我錯過了什么?

我懷疑您的回溯例程會打印出您已經釋放的節點。 (在關於入隊的評論之后的 free() 。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM