6 Replies - 474 Views - Last Post: 13 August 2012 - 02:39 PM Rate Topic: -----

#1 attr-x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-August 12

Declaring variables inside a time critical callback function

Posted 13 August 2012 - 01:21 PM

I know that in a time critical callback function like a callback to process audio buffers, you should not declare variables. But if I look at examples (for instance the portaudio tutorial they are still declaring pointers and loop variables inside it.

What I want to know is: does the rule only count for structures and containers, or should I also avoid declaring ints and floats inside such a function?

In a simple example it won't matter anyway, i guess. But in a real program, where you might process a few hundred buffers on each callback, it might add up. Or are compilers optimized for that?

Is This A Good Question/Topic? 0
  • +

Replies To: Declaring variables inside a time critical callback function

#2 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Declaring variables inside a time critical callback function

Posted 13 August 2012 - 01:29 PM

> I know that in a time critical callback function like a callback to process audio buffers, you should not declare variables.

How do you 'know' this?

> What I want to know is: does the rule only count for structures and containers, or should I also avoid declaring ints and floats inside such a function?

This sounds like a misunderstood concept. Explain what you think you know.
Was This Post Helpful? 1
  • +
  • -

#3 attr-x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-August 12

Re: Declaring variables inside a time critical callback function

Posted 13 August 2012 - 01:43 PM

The portaudio tutorial (linked above) states: "as a rule of thumb, don't do anything like allocating or freeing memory, reading or writing files, printf()" and "The Audio Programming Book" says the same.

Declaring a variable allocates memory. So if I write:

void update() {
  for (int i = 0; i < 1024; i++) {
    // do something
  }
}



I am allocating memory for i. But i could also have the function inside a class, and declare 'i' inside the class also. Like this:

class foo {
  int i;
  void update() {
    for (i = 0; i < 1024; i++) {
      // do something
    }
  }
};


That way, if I once declare an object of this class, there will not be memory allocation for 'i' every time i call the update function.

Only, I am not sure if this is needed.
Was This Post Helpful? 0
  • +
  • -

#4 nuclearfroggy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 82
  • View blog
  • Posts: 167
  • Joined: 04-August 08

Re: Declaring variables inside a time critical callback function

Posted 13 August 2012 - 02:01 PM

What they mean by not allocataing memory is not taking memory from the heap,i.e. using malloc or new. When you declare a variable then it is known about from compilation and uses memory from the stack. Declaring variables will not create issues, especially when it's just a single float or integer.
Was This Post Helpful? 2
  • +
  • -

#5 attr-x  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 13-August 12

Re: Declaring variables inside a time critical callback function

Posted 13 August 2012 - 02:04 PM

Thanks. That was what I needed to know.
Was This Post Helpful? 0
  • +
  • -

#6 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,181
  • Joined: 21-June 11

Re: Declaring variables inside a time critical callback function

Posted 13 August 2012 - 02:08 PM

View Postattr-x, on 13 August 2012 - 10:43 PM, said:

Declaring a variable allocates memory.


Not in any sense that costs time. When people talk about allocating memory, they're talking about things like malloc or new.

Declaring variables simply increases the size of the stack frame, it doesn't cost any additional time. Calling a function with 10 local variables, doesn't take any longer than calling a function with no local variables.

The only time when creating a variable takes time is in case of non-primitive types with non-trivial constructors, in which case the time it takes depends on the constructor you're calling.
Was This Post Helpful? 2
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3470
  • View blog
  • Posts: 10,703
  • Joined: 05-May 12

Re: Declaring variables inside a time critical callback function

Posted 13 August 2012 - 02:39 PM

And if the non-trivial constructors allocate memory on their own.

Cheap allocation:
char buffer[256];



Expensive allocation:
std::vector<char> buffer(256);



The cheap allocation just bumps up (actually bumps down) the stack pointer and the compiler coalesces all the bumps to make it as single bump to the stack pointer. But still you are only touching a single register. The expensive allocation goes and calls new under the covers.

Although now frowned upon, the old technique to get a dynamically sized buffer without the overhead cost of malloc() was to to call alloca(). You can search on your own why this is a bad idea in general, but if you know exactly what you are doing it can be a lifesaver.

This post has been edited by Skydiver: 13 August 2012 - 02:57 PM

Was This Post Helpful? 3
  • +
  • -

Page 1 of 1