Sign in to follow this  

Static or not??

This topic is 4841 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

if i dont repetitive re initialization of a variable, and i am planning to call my function often enough that it isnt wasted memory, should i make all my variables static, because doesnt it waste CPU time re creating and destroying the variable every time i call the function?

Share this post


Link to post
Share on other sites
This approach is only safe if you don't plan to use multi-threading.
Can you provide a small sample on how you plan to use the static variables? I cannot think of many reasonable use-cases for this.

Share this post


Link to post
Share on other sites
a static variable is just a variable that stays in memory after the function is called and is re used when you call that function again.

presumably, if your variable isnt static, the computer needs to find a place for the variable at the start of the function, and then designate it as overwritable when the function is over, this seems like a waste of time to do all this if you plan to call the function very often so you wont really be able to make use of the extra space you provide with the regular variables, so it makes sence to me to use static variable for commonly called functions.

sumone plz prove me right or wrong, thanks

Share this post


Link to post
Share on other sites
No, it doesn't make sense to make variables static. If you mark a variable as static there is space allocated in the actual executable module for it to retain the value between calls. Because the variable itself is not located on the stack, you can actually screw up cache performance since the variable itself is not locally stored on the stack.

Additionally, a variable that retains its value between invocations is harder to debug. Your functions should not have side effects if they don't need to. They should do one thing well and only modify the smallest amount of data necessary.

You are barking up the wrong tree entirely. Stack allocation is extremely fast - merely a subtraction from the current stack base pointer. Use a profiler to find real bottlenecks, not imaginary ones.

Share this post


Link to post
Share on other sites
I am sorry if I rubbed you the wrong way, but I am a bit brash.

The fact remains that simple stack allocation is blindingly fast. If you are allocating an object that does nontrivial operations in the constructor (such as allocating memory) then obviously the speed is going to be diminished by that. Much like some here mistakenly believe inlining to be a silver bullet, static variables will not solve your performance problems.

Share this post


Link to post
Share on other sites
oh, i know that algorithim refining is by far the best way to optimize, and i am having no preformance issues, im just a new coder and am in the process of developing my own coding style, and I like to question conventions if they are not clear to me, but i still dont understand how using a static variable can be bad if you are overwriting the variable with a time specific value anyway at the beginning of the function then why initialize it over and over?

Share this post


Link to post
Share on other sites
Because the very fact that the variable's value is "saved" somewhere between function calls means it may be able to change without you knowing, such as from a buffer overrun (think if a static buffer was allocated right next to the variable in question) or another thread changed it without you knowing.

It's more a matter of "there are less things that can go wrong." Also, you don't need space saved for the variable in the static data portion of the executable. Putting every local into the static data section of the compiled image is going to waste a ton of space.

Share this post


Link to post
Share on other sites
Quote:
Original post by _Phalanx_
oh, i know that algorithim refining is by far the best way to optimize, and i am having no preformance issues...
Premature optimization is the root of all evil - Dijkstra.

New programmers fall into the error of looking for speed where it doesn't exist, largely because that's all they hear intermediate programmers talk about. Advanced programmers know that correctness and maintainability are paramount for projects with meaningful lifespans. Game programming breaks with proper practice for the sake of speed; don't build your habits on the basis of game programming, as it will impact you negatively in all other types of software.

My recommendation is to learn the right way to program first, and then learn when to break the rules. Making variables static should be a semantic decision. Does it change the normal meaning of the code if the variables are static? Then don't do it.

Share this post


Link to post
Share on other sites

This topic is 4841 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this