Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


static variables

This topic is 5622 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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)?

Share this post

Link to post
Share on other sites
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]

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!