Skip to main content
gamedev.net
🔒 Locked

static variables

Started by matt_j Jan 29, 2003 at 12:09 AM 0 replies 250+ views
Original Post
matt_j
matt_j
Are using static variables a good programming practice? I am currently using them for small tasks like object animation... well, anyway, are they relitively fast on a memory and processor level (I am also using them inside member functions)?
niyaw
niyaw
if they are class instances and have constructors: statics will be faster, but you won't notice the difference unless construction is expensive
if they are built-in types or pod types: there will be no appreciable difference in performance

note that static variables make your function non-reentrable (it cannot call itself, directly or indirectly, and it cannot be executed by multiple threads simultaneously*).

use static variables when it makes sense, that is, when you want to persist state across function calls. be aware of restrictions that statics impose on the use of your function.

if you can choose between statics and locals, go for locals. it will make your code more usable (see above) if you or someone using your code ever want to use recursion or multithreading.

* unless calls are synchronized.

[edited by - niyaw on January 29, 2003 1:13:13 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.