static variables instead of ordinary variables?

Is it more expensive to use too many static variables instead of ordinary variables? If yes, then how? explain it a bit plz

Whether or not you should use static variables is less about memory consumption and more about code modularity.

Static variables are less modular. They're effectively global variables (just with limited scope)... which greatly limits their functionality.

Advantage to using static vars
- its contents is remembered between function calls.

Disadvantages to using static vars
- Its initialized only the first time the function is called
- It makes the function unsafe for multithreading
- It makes the function unable to be re-entrant (it can't call itself or call another function which calls itself)
- It potentially disrupts logic if you have it being called from more than one area of code at a time ( hard to explain.. OP had that problem in this thread: http://www.cplusplus.com/forum/windows/138595/2/ ... in the 1st post on the 2nd page, his "problem 2")




So yeah -- you generally don't want to use statics unless you have a very good reason to.
Last edited on
Topic archived. No new replies allowed.