[C] memory allocation in perform routine (problem)

May 21, 2010 at 12:10pm

[C] memory allocation in perform routine (problem)

Hi everybody,
I’ve a problem that I can’t solve, although the help of colleagues and university mates. I hope in your support. This is for my thesis’s training, please! :)

In my perform routine I need to save in a vector the samples to make some stuff (interpolation). No problem: I allocate the memory I need…

The problem is that i cannot succeed in compiling the following code due to the allocation of memory. In fact the C pointer and the malloc instruction are ok, but if I try to compile the instructions that are in the code as a comment (the ones that work with the memory allocated) they generate “waterfall” errors on the instructions following (non-existent errors).

I cannot understand the causes of the problem. I suppose it’s not for the MaxMSP’s library, a C mistake, so what would be the problem???

Here’s the code:
[code]
t_int *distexp_performScalar(t_int *w)
{
// w[0] &distexp_perform
t_distexp *x = (t_distexp *)(w[1]); //object's pointer
t_float *inL = (t_float *)(w[2]); //input array pointer
t_float *outL = (t_float *)(w[3]); //output array pointer
int n = (int)w[4]; //signal vector

t_float *list1samples;
list1samples = (t_float *) malloc(n*sizeof(t_float));

while (n--){ //per sample . . .

//Calculations...
t_float effecondi1 = x->fd1;
t_float esponentedik1 = 3. * effecondi1; //
[...]
t_float segno1 = sgn(input1); //
t_float result1 = segno1 * parentesi1;
//*(list1samples)= result1;
//list1samples[0] = 0.1;
//list1samples[n] = result1;
//*(list1samples+(n-1)) = result1;

//other instruction that generates errors
t_float effecondi1 = x->fd1;
t_float esponentedik1 = 3. * effecondi1;
t_float k1 = exp(esponentedik1);
t_float modulo1 = fabs(*inL);
t_float modulonegativo1 = 0 - modulo1;
[...]
}
return w + 5;
}
[/code]
Note that if i close immediately ofter the } of the while with the incriminate instructions it compiles!.

#50493
May 21, 2010 at 5:11pm

I confess that I did not really look at your code, so I don’t have an answer for you (maybe later…)

though, I would strongly advise against allocating memory in the perform method (besides the fact that you don’t seem to free it, and this can quickly lead to disaster!) – imho you would do much better by putting your malloc() in the dsp method, rather than perform.

moreover, though I am not sure about the actual difference, in general sysmem_newptr() is used instead of malloc().

cheers
aa

#181127
May 21, 2010 at 10:05pm

I can second that using malloc in the perform routine is a bad idea.

As n is not likely to be that big and it seems like you don’t need to keep it after the routine has exited. I might be tempted to dynamically allocate the array like this…..

t_float list1samples[n];

or you could just allocate a fixed array the size of which you do not expect to exceed (like 4096 for example).

As for the errors – it’s a bit hard to know without the exact error message, or the entire perform routine. I’d guess you have unmatched brackets, or something similar – the sort of problem that can be frustrating to find, as I can’t see anything obviously wrong with the commented out lines. I doubt from what you’ve posted that it is anything to do with allocating memory.

Alex

#181128
May 22, 2010 at 8:00pm

Like others have said, allocating memory in your perform function is a bad idea. Also, if you do dynamically allocate memory that way, you need to free the memory somewhere. Also, you need to check to make sure that malloc returns without an error.

Having said that, this will be hard to diagnose without:
a) the full source you are trying to compile.
b) the error message your compiler gives you.

From what you have posted… you are redefining effecondi1 and esponentedik1.

(Andrea: sysmem_newptr is just a wrapper around malloc, intended for portability purposes. So yes, you should use sysmem_newptr, but no, it shouldn’t make any difference.)

/brian

#181129
May 23, 2010 at 10:51pm

To add to the sysmem_newptr debate – there are reasons not to use it (like if you care about memory alignment etc.), but whatever you decide the important thing to remember is to match the correct allocation and deallocation functions – you shouldn’t try to free memory allocated with sysmem_newptr with free() for instance).

Alex

#181130

You must be logged in to reply to this topic.