Sign in to follow this  
pascalosti

static float?

Recommended Posts

Can someone give me a simple define on: "static" float and why/ or when I would use it. The code below made me rethink what a static is. (i understand the code)

// code from "How not to program in C++" book

1 /***********************************************
  2  * Test question:                              *
  3  *     What does the following program print?  *
  4  *                                             *
  5  * Note: The question is designed to tell if   *
  6  * the student knows the difference between    *
  7  * automatic and static variables.             *
  8  ***********************************************/
  9 #include <stdio.h>
 10 /***********************************************
 11  * first -- Demonstration of automatic         *
 12  *      variables.                             *
 13  ***********************************************/
 14 int first(void)
 15 {
 16     int i = 0; // Demonstration variable
 17
 18     return (i++);
 19 }
 20 /***********************************************
 21  * second -- Demonstration of a static         *
 22  *      variable.                              *
 23  ***********************************************/
 24 int second(void)
 25 {
 26     static int i = 0;  // Demonstration variable
 27
 28     return (i++);
 29 }
 30
 31 int main()
 32 {
 33     int counter;          // Call counter
 34
 35     for (counter = 0; counter < 3; counter++)
 36         printf("First %d\n", first());
 37
 38     for (counter = 0; counter < 3; counter++)
 39         printf("Second %d\n", second());
 40
 41     return (o);
 42 }


Share this post


Link to post
Share on other sites
A standard variable (assumed to be "auto" storage class unless you explicitly state "static") is re-initialized every time the function is called, and is set to garbage unless you specify an initial value.

A static variable is only initialized once - when the function is first called - and defaults to zero unless you specify an initial value. Static variables are good for (among other things) profiling your code by determining how many times a given function was called.

Share this post


Link to post
Share on other sites
Function static variables don't lost their values between calls, they only get initialised the first time the function gets called. Automatic variables only exist when the function is called and get a new initial value every time.

They are mainly a relic from C, as C++ can use function objects to have the same (or better) effect.

An example would be a random generator function. A simple random function would have a seed static variable and would compute its return value by shuffling the bits of the seed and storing the result. The seed could be first initialised to a timer value to provide a reasonably unique seed.


int myRand()
{
static int seed = time(NULL);
seed = bitshuffle(seed);
return seed;
}


Most real random generators will have the seed outside the function to allow one to manually set the seed, but this is the general idea.

In c++ (your code is C but the book mentions c++) you could use a function object which has the same advantages (retain the seed state between calls) but you can have more than one seed state active:


class MyRand
{
public:
MyRand( int initSeed )
:
seed(initSeed)
{
}

int operator()()
{
seed = bitshuffle(seed);
return seed;
}
private:
int seed;
};

// use
void foo()
{
MyRand myRand( time(NULL) );

int somevalue = myRand(); // use like a function
}


Function static variables are rare, after all they are a special form of global. In addition to this, they are not thread safe. When to use them? Static variables are one of those features of C++ that once you know what they are, you know when to use them. But do keep in mind that function objects are usually the better choice ( they can be made thread local for example ).

Share this post


Link to post
Share on other sites

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