Real-time audio : passing by value [closed]

In real time audio, one cannot program the way they usually do: with new delete or even malloc: it introduces glitches when creating memories.

By if I pass by value, a copy is made right, so let's suppose I pass a vector by value, its value will be copied, thus creating a memory thus it's forbidden with real-time audio?

Is it true?

So I have to pass the vector by reference and copy it using pre allocated buffers?

Or is it safe to pass by value?

Jeff

closed as off-topic by Paul R, lennon310, jojek♦, MBaz, Matt L.Mar 4 '15 at 9:02

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "General programming questions are off-topic here, but can be asked on Stack Overflow." – Paul R, lennon310, jojek, MBaz, Matt L.
If this question can be reworded to fit the rules in the help center, please edit the question.

This is clearly a programming question, but to give you an answer...

its value will be copied, thus creating a memory thus it's forbidden with real-time audio?

When you pass an argument by value, it is copied onto the stack, so there is no memory allocation.

so i have to pass the vector by reference and copy it using pre allocated buffers?

Or write your code in a way that data can be safely passed by reference without having to be copied anywhere?

safe to pass by value?

Safe but waste of resources.

• okay but your solution is limited to the array size, i'll have to pass by reference if the array is too big. – ionone Jun 20 '14 at 7:49
• Why is my solution limited to array size? Could you post a more concrete example of what you need to do? – pichenettes Jun 20 '14 at 7:54
• if passing a too big array by value, there could be stack oveflow, no? – ionone Jun 20 '14 at 7:55

If you pass audio data by value on the stack, a copy will still have to be made, as the parameters and local variables on the stack are transient (alive only during the time-limited subroutine set-up, call and/or callback). So better to preallocate buffers, and pass them by reference, to avoid extra copies in and out of transient memory.

Also, sufficient stack growth could potentially invoke the OS virtual memory system, which might also add to real-time latency, so best to keep large amounts of data off the local variable and parameter stack.

In dynamic message passing languages, such as Objective C on iOS, it might also be necessary to avoid passing data using any object messages that might miss the message lookup cache inside real-time audio callback threads.