将值放入我的数组后,一步后(感谢 gdp)数组包含垃圾。之后唯一的步骤是将参数传递给函数:
struct Vertex;
typedef struct Vertex Vertex;
struct Vertex {
int sides[2][LENGTH];
int ends[2];
Vertex *children;
Vertex *parent;
};
void move(Vertex *node, int side, int place) {
(38) int handfull = (*node).sides[side][place];
.....
}
int blah(Vertex *node, int side) {
.....
(103) *((*node).children + i) = init_child(node);
(104) move((*node).children + i, side, i);
(105) blah((*node).children + i, opposingside);
.....
}
gdb 告诉我以下内容:
(gdb) print (*node)
$7 = {sides = {{5, 5}, {0, 5}}, ends = {0, 1},
children = 0x7fffffffdfa0, parent = 0x7fffffffe110}
(gdb) print node
$8 = (Vertex *) 0x7fffffffe0c0
(gdb) print ((*node).children + i)
$9 = (Vertex *) 0x7fffffffdfa0
(gdb) print *((*node).children + i)
$10 = {sides = {{5, 5}, {0, 5}}, ends = {0, 1},
children = 0x7fffffffdf20, parent = 0x7fffffffe0c0}
(gdb) step
move (node=0x7fffffffdfa0, place=0, side=0) at mancala.c:38
38 int handfull = (*node).sides[side][place];
(gdb) print node
$11 = (Vertex *) 0x7fffffffdfa0
(gdb) print (*node)
$12 = {sides = {{-8160, 32767}, {4196818, 0}}, ends = {0, 1},
children = 0x7fffffffdf20, parent = 0x7fffffffe0c0}
我在第 104 行中断(称为move
)。之后,(*node).children + i
指向顶点结构,如 $9 和 $10 所示。我迈出了一步move
。这应该将我的顶点地址、返回地址等推入堆栈并开始在move
函数中工作。查看 $11,我们看到 Vertex 地址与 $9 相同,这很好。但是,在 $12 中,数组.sides
现在充满了垃圾。其他一切都很好,但只是那个阵列充满了垃圾。
这是如何一步完成的?我一次只能调试一个步骤,所以我不知道现在该做什么。
编辑:这是 init_child 函数:
Vertex init_child(Vertex *node) {
Vertex children[LENGTH];
int i;
int j;
Vertex child = (*node);
for (j = 0; j < 2; j++) {
for (i = 0; i < LENGTH; i++)
child.sides[j][i] = (*node).sides[j][i];
child.ends[j] = (*node).ends[j];
}
child.children = children;
child.parent = node;
return child;
}
这是初始化第一个顶点的 main() 函数:
int main(int argc, char **argv) {
Vertex children[LENGTH];
Vertex head = {.sides = {{4,4}, {4,4}}, .ends = {0}, .children = children, .parent = NULL};
blah(&head, 1);
return 0;
}