0

I have the following function:

edge** graph_out_edges(graph* g, int src) {
    int i;
    int num_edges = 0;
    edge** es = (edge**) malloc(sizeof(edge*) * g->num_edges);

    for (i = 0; i < g->num_edges; i++) {
        if (src == g->edges[i]->src) {
            es[num_edges++] = g->edges[i];
        }
    }

    es[num_edges] = NULL;
    return es;
}

I add a breakpoint to the function using b graph_out_edges, run the program using r, and then continue (c) twice (I get a segfault if I continue again). I then n through the function until it moves to the command just after the call to the function

edge** new = graph_out_edges(g, min->dest);

p new[0] and p new[1] give valid edges (the members are populated), and p new[2] gives 0x0, as expected. I then type r to restart the program, again continuing twice, but this time I then type ret (confirming I want to return), type n to execute the assignment, but now when I type p new[0] I get

Cannot access memory at address 0x2

(Just for clarity, p new now says $10 = (edge**) 0x2)

Any suggestions on why there is this discrepancy between the return value when "nexting" through the function manually and forcing a return?

Sean Kelleher
  • 1,952
  • 1
  • 23
  • 34
  • 1
    you do not check the value of `es` after the malloc. Make sure that malloc does not return NULL. – mathk Dec 13 '13 at 10:20
  • 1
    [Please don't cast the return value of `malloc()` in C](http://stackoverflow.com/a/605858/28169). Also, if all of the incoming edges match, the loop will end with `num_edges == g->num_edges` and the final sentinel-write of `NULL` will overflow the buffer. Danger! – unwind Dec 13 '13 at 10:49

1 Answers1

0

In the function, if you guarantee the value of es is right, then after the call, the value of new should be right.

Perhaps, I suppose that,

First, check the es;
Then, compare the return value of new with es.
Tao T
  • 66
  • 4
  • This highlighted an incorrect assumption I had, that `ret` in `gdb` executed the function until natural return, but in fact it discards the stack frame, so I'm going to mark this as correct. – Sean Kelleher Dec 13 '13 at 10:44
  • As an additional note, `finish` in `gdb` is the command I should have been using, which continues execution until natural return. – Sean Kelleher Dec 13 '13 at 10:46