Hi Jan, Thanks! Actually, with your help I have now solved the second problem too. Comments below:
On 21 June 2015 at 17:23, Jan Strube <[email protected]> wrote: > I tried your code and could reproduce the problem: I have to suggestions: > -- Julia is 1-based, so drop the 0 in your unsafe_load. The default > parameter is 1, which is what you want in this case. > Ah! I thought that the "0" would be interpreted by C. Yes, dropping the "0" made it work. I also understand now why Nmax() was returning "7" -- the "0" was causing Nmax() the return the value of "N". > -- I can't really understand what your function is supposed to do (and my > C is a bit rusty), but this simpler version works for me. > I suggest to start there and add the additional functionality of > particles_add bit by bit. > My C is a bit rusty too. Your example helped. It turns out that my C code was broken. I foolishly set N and Nmax to non-zero values because I wanted to test the N() and Nmax functions. This was foolishbecause the "particles" array was not allocated. In the particles_add() function I have this: while (Nmax <= N){ Nmax += 128; particles = realloc(particles,sizeof(struct particle)*Nmax); } particles[N] = pt; N++; The idea is that whenever I fill up the "particles" array I allocate new memory, so it can keep growing and accepting new particles. It was foolish to set N = 7, Nmax = 9 because I had not allocated any memory at all. So when the code gets to "particles[N] = pt" it tries to write to unallocated memory, which leads to the segfault. The fix is to set N and Nmax to 0 from the beginning. int N = 0; int Nmax = 0; With this, and the other fix you suggested earlier, the code works: % julia ... julia> include("foo.jl") add (generic function with 2 methods) julia> N() 0 julia> Nmax() 0 julia> add(x=0) julia> N() 1 julia> Nmax() 128 Thanks again. Cheers, Daniel
