Forums > Dev

Can I pass an array of structs from object to object?

April 9, 2014 | 4:05 am

Can I pass an array of structs from object to object? Say I want to pass an array of t_custom which has four integers in the structure. Do I have to extract each integer from the structure and pass it as an array of A_LONGs? Or is there a way to pass the array of structs directly? Thanks


April 10, 2014 | 6:07 am

If you’re passing data between objects you’ve written yourself, you can just pass a pointer to the struct. Pointers generally look like longs.

This is a C-based approach. Java and JavaScript aren’t supposed to let you access pointers directly.


April 18, 2014 | 5:32 pm

Thanks for your answer Peter.

For some reason max crashes every time I try to do this. This is the code:

In sending object:

` t_atom av[2];
atom_setlong(av,(long)((x->MNLength)/2)); //length of the array, 100% correct one
atom_setlong(av+1, (long)(x->rMN_outData)); //pointer to an array of structs
post("outadress = %d, length %d",x->rMN_outData,(x->MNLength)/2);
outlet_anything(x->outlet_4, gensym("IBnote"), 2, av);`

In receiving object:

int numOfNotes = atom_getlong(argv);    //getting the length
    post("inadress = %d, length %d",atom_getlong(argv+1),numOfNotes);  //address and length same as the one sent
    sysmem_copyptr((t_Note*)atom_getlong(argv+1), x->ITHN_outData, numOfNotes*sizeof(t_Note));// crashing here

Is there anything I am doing wrong? The code worked before, when I passed the array using a conventional method, by setting each element of the struct as t_atom and passing an array of atoms. I thought I will try this method as it avoids the need of converting to and from t_Note struct but for some reason it crashes.


April 18, 2014 | 5:52 pm

Ah, found it, I have to initialise x->ITHN_outData first.


Viewing 4 posts - 1 through 4 (of 4 total)