2

After putting values into my array, one step later (thanks gdp) the array contains garbage. The only step afterwards is passing parameters to a function:

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 tells me the following:

(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}

I break at line 104 (called move). After, (*node).children + i points to a Vertex structure as evidenced by $9 and $10. I make one step into the move function. This should push my Vertex address, the return address, etc. onto the stack and start working in the move function. Looking at $11, we see that the Vertex address is the same as in $9, which is fine. However, in $12, the array .sides is now full of garbage. Everything else is fine, but just that array is full of trash.

How has this happened in one step? I can't debug any further than one step at a time, so I don't know what to do now.


EDIT: Here's the init_child function:

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;
}

And here is the main() function that initializes the first Vertex:

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;
}
1
  • 2
    Can you post your full code please? Or a cut-down example that still demonstrates the problem.
    – simonc
    Aug 13, 2013 at 16:04

1 Answer 1

6

The problem is that init_child() is initializing children with the address of an array that was created at function scope. When the function returns, the array is no longer valid, and so the child structure returned is pointing to an invalid object.

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;
}

Instead, the routine should dynamically allocate the memory so that the child points to memory with a lifespan that continues to survive after the function returns.

Vertex init_child(Vertex *node) {
/*...*/
child.children = malloc(LENGTH*sizeof(*child.children));
child.parent = node;
return child;
}
3
  • THANKS! This solved it. I would up-vote, but I don't have enough rep, apparently. <3 <3 Aug 13, 2013 at 16:28
  • I actually created space before calling init_child and passed that stuff to init_child: Aug 13, 2013 at 16:33
  • That's find too, but if LENGTH is large or if Vertex is large, you risk exceeding the amount of memory the OS provided for the stack.
    – jxh
    Aug 13, 2013 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.