Skip to main content
GameDev.net gamedev.net
🔒 Locked

A question about static variables...

Started by Moe May 22, 2002 at 6:18 PM 35 replies 650+ views
Original Post
Moe
Moe
I seem to be having a little trouble with a static variable... From my little understanding of static variables, I understand that the memory for a static variable in a function is not de-allocated until its told to. Now lets say I have a function with a static variable in it:
  
void func(void)
{
static float blah = 0;

//do stuff to blah


}
  
Now whenever I call the function, does the value of blah get reset to 0, or does it retain its last value from the last time the function was called? What I am trying to do is print the updated frames per second out only once per second, not update it every single frame. Now assuming that I have a one second timer that works properly, could I do something like this:
  
int Render()
{
static float fps = 0;

if(its time to update the fps)
    //update the fps

    fps = something;
    outputfps(fps);
else
    output the fps from the last time it was updated
    outputfps(fps);

//return whatever

}
  
Would the previous suedo-code (assuming that everything works properly) print out the fps from when it was last updated, or would it just print out 0? Moe''s site
Big Sassy
Big Sassy
The former. It would print out the last value. This:

static float blah = 0;

Only happens once (when it''s initialized). After that it only changes when you change it.

***********************
          
Stoffel
Stoffel
You really need a faster computer. Because if writing down a post and sending it to this forum and waiting for a reply is faster than compiling a test program and figuring it out yourself, I have no idea how you even connect to the internet.
Puzzler183
Puzzler183
LOL!

Can we flame him?

Just kidding but you really should try to do some really simple program tests like that by yourself.
Big Sassy
Big Sassy
I just find it interesting that he''s asking a question as simple as this considering his skill level.

***********************
          
Moe
Moe
quote:

The former. It would print out the last value. This:

static float blah = 0;

Only happens once (when it''s initialized). After that it only changes when you change it.


Thank you. I just wanted to confirm what I thought was true.

quote:

Can we flame him?


No. Try taking a little look at how long I have been a member here. Have a little respect for people!

quote:

Just kidding but you really should try to do some really simple program tests like that by yourself.


Point taken.
quote:

You really need a faster computer. Because if writing down a post and sending it to this forum and waiting for a reply is faster than compiling a test program and figuring it out yourself, I have no idea how you even connect to the internet.


The reason I bothered to post is because I thought someone here might have some little tidbit of information that I didn''t know about static variables yet. I have heard some pretty interesting things about the simplest posts here. Its always nice to know a little more than whats required. And yes, I do need a new computer.

Not only that, but some newbie might just have enough brains to search for posts on static variables if they have a similar question, and it would come up with this thread.


Moe''s site
Void
Void
quote:
Original post by Moe
From my little understanding of static variables, I understand that the memory for a static variable in a function is not de-allocated until its told to.


Who tells it to? You?

The typical lifetime of the static variable inside functions is similar to registering with atexit(). So you do suffer destruction order problems if you depend on it.

Seifer
Seifer
It''s regrettable that forum members are often pressured to justify their questions when they post.
I hate signatures.
bishop_pass
bishop_pass
quote:
Original post by Seifer
It''s regrettable that forum members are often pressured to justify their questions when they post.

It is indeed. I have been saying similar things in some pretty heavy debates the last two weeks. Let this remain a great place to talk about coding. Talking about coding is more interesting and productinve than talking about whether one should talk about coding.

Sometimes talking about coding is more enjoyable than sitting all alone and compiling code to get your answers.
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in
elis-cool
elis-cool
On the topic of statics I read this somewhere and was wondering if it was true:

static char MyWord[10];

Would the array MyWord be initialized to null(all parts ie. 0-9)? I would think it wasnt but maybe it is; as some old C thing...

CEO Plunder Studios

[edited by - elis-cool on May 23, 2002 5:05:50 AM]
[email=esheppard@gmail.com]esheppard@gmail.com[/email]
Seifer
Seifer
quote:
Original post by bishop_pass
It is indeed. I have been saying similar things in some pretty heavy debates the last two weeks. Let this remain a great place to talk about coding. Talking about coding is more interesting and productinve than talking about whether one should talk about coding.

Sometimes talking about coding is more enjoyable than sitting all alone and compiling code to get your answers.


I''ve read those debates and I totally agree. Unfortunately, the elitist attitude seems to dominate the programming community in general. I''m glad you''re a moderator. I like the "Don''t wanna help? Don''t post" rule.
I hate signatures.
Alimonster
Alimonster
quote:
Original post by elis-cool
On the topic of statics I read this somewhere and was wondering if it was true:

static char MyWord[10];

Would the array MyWord be initialized to null(all parts ie. 0-9)? I would think it wasnt but maybe it is; as some old C thing...

I whipped up a little test program and it seems that they''re initialised to the NUL char (''\0''):

  #include <iostream>
using namespace std;

void func()
{
static char MyWord[10];

for (int i = 0; i < 10; ++i)
{
if (MyWord[i] != ''\0'')
{
cout << "Not all nul char" << endl;
return;
}
}
cout << "Looks like it''s all the nul char" << endl;
}

int main()
{
func();
return 0;
}

It gave the all-nul response in both debug and release modes. As always, though, it''s better to intialise the array yourself to be safe.
Moe
Moe
quote:

Who tells it to? You?

The typical lifetime of the static variable inside functions is similar to registering with atexit(). So you do suffer destruction order problems if you depend on it.


No one in peticular told me, I just thought thats how it worked. I''m afraid I''m not really familiar with atexit(), but thanks for telling me.



Moe''s site
Void
Void
quote:
Original post by Moe
[quote]
Who tells it to? You?

The typical lifetime of the static variable inside functions is similar to registering with atexit(). So you do suffer destruction order problems if you depend on it.


No one in peticular told me, I just thought thats how it worked. I''m afraid I''m not really familiar with atexit(), but thanks for telling me.


I''m not flaming you. I was pointing out the fact you said the static variable in the function not deallocated until its told to. The "who" is the sentence is what I''m addressing.

Peace.
fallenang3l
fallenang3l
quote:
Original post by Alimonster
I whipped up a little test program and it seems that they''re initialised to the NUL char (''\0''):

It gave the all-nul response in both debug and release modes. As always, though, it''s better to intialise the array yourself to be safe.


No need to do anything yourself in this situation because this is the standard C++ behavior for all static variables.
a person
a person
try

    
// initilaize first element to null

static char blah[10]={0,};
// useful since you can initalize a the first few elements in an

// array or array of structs




[edited by - a person on May 23, 2002 10:32:58 PM]
tebriel
tebriel
quote:
Original post by Seifer
Original post by bishop_pass
It is indeed. I have been saying similar things in some pretty heavy debates the last two weeks. Let this remain a great place to talk about coding. Talking about coding is more interesting and productinve than talking about whether one should talk about coding.

Sometimes talking about coding is more enjoyable than sitting all alone and compiling code to get your answers.

I've read those debates and I totally agree. Unfortunately, the elitist attitude seems to dominate the programming community in general. I'm glad you're a moderator. I like the "Don't wanna help? Don't post" rule.


I'm glad I'm not the only one to notice the "elitism", and that I'm not the only one who hates it.....

[edited by - Tebriel on May 24, 2002 2:39:25 AM]
Seifer
Seifer
quote:
Original post by Tebriel

I''m glad I''m not the only one to notice the "elitism", and that I''m not the only one who hates it.....




One thing I wonder though is when, how and why that elitism developed ; I surmise it has not always been there.

I hate signatures.
Puzzler183
Puzzler183
I''m not an eleitist but for something like that, I would just punch it into my compiler. It would take less time for me that way, so in my opinion, that is the way to go.
bishop_pass
bishop_pass
quote:
Original post by Puzzler183
I''m not an eleitist but for something like that, I would just punch it into my compiler. It would take less time for me that way, so in my opinion, that is the way to go.

Everything isn''t about which is the fastest way to answer a question. Sometimes a little discussion is just plain more enjoyable, or opens up a new avenue of questioning, or reveals a new tidbit of knowledge. It is supplemental to testing, even if the person raising the original question thought to test it out via compilation. But maybe they they just didn''t think to try that.

_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in

Topic Locked

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

Sign in to reply to this topic.