Try initialising the variables to 0 first. Probably not the fault but other things can go wrong now.
var i = 0; var j = 0; var k = 0;
var num_nodes = 0;
How did you check the value num_nodes? If you used printf("%i", num_nodes); then indeed you got an odd number. printf("%i", (int)num_nodes); should solve that.
Click and join the 3dgs irc community! Room: #3dgs