array of Atoms

Jan 6, 2010 at 6:43pm

array of Atoms

Dear all

I want to create an array of atoms that I can reuse. I put a handle in my object structure and I assign the memory at instantiating time that way:

x->array_out = (t_atom *)getbytes(cell_nb * 2 * sizeof(t_atom));

but it does not seem to work, I hang Max all the time.

Any help is welcome, I need the object to be Max4.6 compatible so new methods are forbidden ;-

pa

#47609
Jan 6, 2010 at 9:36pm

Are you sure cell_nb isn’t too large for getbytes? Does it return a valid pointer, or 0?

#171214
Jan 6, 2010 at 10:13pm

Dear Martin

cell_nb is 32, so it is plenty within the range (512 byte only). I do seem to get a proper pointer but this code seem to be th problem.

atom_setfloat(x->array_out[j++],x->cell_array[i]->freq);
atom_setfloat(x->array_out[j++],x->cell_array[i]->amp);

I’ll continue to investigate, but this code worked fine before, so I am quite puzzled…

pa

#171215
Jan 6, 2010 at 10:18pm

…tried running it in runtime under the debugger?

#171216
Jan 6, 2010 at 10:29pm

tried it, and I made it work by doing this instead:

atom_setfloat(x->array_out+j,x->cell_array[i]->freq);
atom_setfloat(x->array_out+j+1,x->cell_array[i]->amp);

strangely it did not accepted x->array_out[j] as a (t_atom *)

I am quite surprised by this but it sorted my problem…

thanks for your help

pa

#171217
Jan 7, 2010 at 4:25pm

I’m surprised the compiler didn’t give you a warning with the first code.

This code:

atom_setfloat(x->array_out+j,x->cell_array[i]->freq);

is the same as this:

atom_setfloat(&x->array_out[j],x->cell_array[i]->freq);

but it is not the same as this:

atom_setfloat(x->array_out[j],x->cell_array[i]->freq);

(note the &)

Glad you got it figured out…
Cheers

#171218
Jan 7, 2010 at 4:46pm

Tim, I am so ashamed… such a midget mistake, thanks for pointing it !

pa

#171219
Jan 7, 2010 at 5:44pm

No need to be ashamed — this kind of pointer snafu in the C language is part of the reason that so many other languages have been invented, and why C makes so many people run in the opposite direction. It snags us all from time to time…

#171220
Jan 7, 2010 at 6:02pm

Thanks for the reassuring word ;-)

Btw I did not get the error before because ext_obex.h was not included in my code. It was still compiling but the external was very unstable…

pa

#171221

You must be logged in to reply to this topic.