2

将值放入我的数组后,一步后(感谢 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;
}
4

1 回答 1

6

问题是使用在函数范围内创建的数组的地址进行init_child()初始化。children当函数返回时,数组不再有效,因此返回的子结构指向一个无效对象。

Vertex init_child(Vertex *node) {
Vertex children[LENGTH];            /* array variable on the stack */
/*...*/
child.children = children;          /* child pointing to local variable */
child.parent = node;
return child;
}

相反,例程应该动态分配内存,以便子进程指向内存,其生命周期在函数返回后继续存在。

Vertex init_child(Vertex *node) {
/*...*/
child.children = malloc(LENGTH*sizeof(*child.children));
child.parent = node;
return child;
}
于 2013-08-13T16:24:02.720 回答