Modifying the atoms passed to a list method


    Sep 01 2019 | 9:14 am
    Hi folks,
    I am coding a regular Max object and I am implementing a 'list' method. So the standard A_GIMME signature is as follows: void myobject_list(t_myobject *x, t_symbol *sym, long argc, t_atom *argv); In the method implementation am I allowed to do something like the following? void myobject_list(t_myobject *x, t_symbol *sym, long argc, t_atom *argv) { for (long i = 0; i < argc; i++) { double f = atom_getfloat(argv + i); f = CLAMP(f, 0.0, 1.0); atom_setfloat(argv + i, f); } outlet_list(x->outlet, NULL, argc, argv); }
    My concern comes from the fact that I am directly modifying the content of the atoms passed to the method and I am not sure if that preserves the integrity of the message passing system.
    The alternative would be to make a copy of the atoms and modify the copy:
    
    void myobject_list(t_myobject *x, t_symbol *sym, long argc, t_atom *argv)
    {
        t_atom temp[OBEX_UTIL_MAX_ATOM_STATIC];
        t_atom *argv2 = atom_dynamic_start(temp, OBEX_UTIL_MAX_ATOM_STATIC, argc);
        
        for (long i = 0; i < argc; i++) {
            double f = atom_getfloat(argv + i);
            f = CLAMP(f, 0.0, 1.0);
            atom_setfloat(argv2 + i, f);
        }
        outlet_list(x->outlet, NULL, argc, argv2);
        
        atom_dynamic_end(temp, argv2);
    }
    The absence of a const qualifier on the t_atom * argument suggests that I am allowed to modify the data, however since the Max API is not always consistent with regards to const-ness, I thought I would just ask.
    I built a simple test case and everything seems to work as expected with both approaches, however I don't know if in a more complex situation I would encounter unexpected bugs.
    So, is there a correct way to implement the above or are the two approaches equivalent? Thanks
    - Luigi

    • Sep 02 2019 | 9:09 am
      You are allowed but probably shouldn't ;-)
      Consider a patch like bellow, if vexpr didn't copy the list before doing the math, you would have strange results coming out the second (right) vexpr.