Dev noobiness: Initializing an empty t_atomarray and appending later

antwan's icon

Hi there,

I'm taking an escapade into the Max SDK and I'm getting on better than I ever thought I would. But still...
I'm quite a noob when it comes to serious programming although I have gone through some decent efforts to learn objective-c a little while back.

Anyways, I wonder if someone could lend me a bit of guidance.

I was trying to use the t_atomarray module but couldn't find any examples using that. I was simply trying to initialize an instance of it and later append atoms to it. But I end up crashing Max every time. Could some one please give a quick hands down on how is it supposed to be used? I would appreciate it a great deal!

As a secondary question: if one intends to store two lists of atoms and later compare them for differences, would you use t_atomarray for storing the set of atoms or something else?

Thanks!

antwan

Timothy Place's icon

If you have some code then it will be easier for us to help diagnose the problem.

As for comparison of lists of atoms, you can store the lists any way you like. From the atom array you can get an atom count and a pointer to the first in the array that you use to iterate through the list. If you were into C++, which it doesn't sound like, then you could store the atoms in a std::vector and write a comparison function for that. Anyway, the answer is "it depends".

If you tale a look at the following code, there is a function called param_list_compare() starting on line 1445 that may be helpful.
http://github.com/tap/JamomaModular/blob/master/implementations/MaxMSP/jcom.parameter/jcom.parameter.cpp

Cheers

antwan's icon

Hi Timothy,

And thanks a lot for helping me out.
Yes, I noticed that no one seems to be using t_atomarray as such. As in your example, all the examples just seem to use the technique of "pointer to first t_atom and count of atoms". This technique was kind of strange to me at first (not being a C programmer) but I did kind of manage to understand it and use it.

I just figured that t_atomarray looks to have some helper functions built-in that might come in handy, so I tried to figure out how to set that up.

First problem was that I didn't see a way to set an "empty" t_atomarray up since the atomarray_new() function expects me to have some atoms lined up. So I was attempting a simple test where whenever a float comes in, it appends it to the end of the t_atomarray. So what I did to try it out was that in the object struct I wrote:

t_atomarray *atar;

and later:

void testext_float(t_simplemsp *x, double f)
{
t_atom *a;
atom_setfloat(a, f);
if (x->atar == NULL)
{
    x->atar = atomarray_new(1, a);
} else {
atomarray_appendatom(x->atar, a);
}
}

Then, as soon as the external is sent a float... max crashes. I can somehow feel that the code is absolutely all wrong (!) but don't know where to look.

Do forgive my amateurness - and thanks for your patience - I think once I understand this I'll be okay for a while again :)

antwan

Timothy Place's icon

Thanks for the example code -- that makes it much easier to see what is going on.

You have:

     t_atom *a;

which is a pointer to an atom -- but this is not actually an atom. That means that the atom_setfloat() call is trying to set an atom that is a pointer to by 'a', except that 'a' is pointing to some undefined location in memory -- thus crashing.

One solution is to do this:

    t_atom a; // not a pointer, a real atom
    atom_setfloat(&a, f);

I don't remember off the top of my head, but you might be able to create an empty atom array with

    x->atar = atomarray_new(0, NULL);

(passing zero atoms and a zero pointer).

Hope this helps!

antwan's icon

Hi there,

Well that definately made it work! Also creating the empty t_atomarray worked exactly as proposed. A million thank yous... now I just need to study what you explained REAL HARD so i REALLY understand it.

Thanks so much,

antwan