Thread: what does static mean?

  1. #1
    Unregistered
    Guest

    what does static mean?

    can someone help me understand what static does?
    I'm a beginner in c++.

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Please give a sample of the instance because it can mean different things.

  3. #3
    Unregistered
    Guest
    for( int x=0 , static x=1 ; i<10 ; x++ )
    for( int y=0 ; y < 10 ; y ++ )
    cout<<x<<y<<endl;

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >can someone help me understand what static does?
    The static keyword has two functions in C++, when the object is in local scope to a function, static means that the object is not placed on the stack and has a lifespan equal to that of the program:
    Code:
    char *function()
    {
      char p[] = "This is a test";
      return p;
    }
    This code will flag a warning and most likely not work because the memory for p is released when the function returns. By declaring p as static however, the function will return the proper value:
    Code:
    char *function()
    {
      static char p[] = "This is a test";
      return p;
    }
    The second usage of static is in a global scope, such as when you are declaring a function prototype. The problem with global scope is that everything has external linkage by default, meaning that other source files can access them and possibly have a naming conflict. By declaring them as static you are telling the compiler that they do not exist outside of this particular source file. It's a good practice in data hiding.
    Code:
    // Only visible to this file
    static char *function()
    {
      // Resides in memory that is not released
      static char p[] = "This is a test";
      return p;
    }
    -Prelude
    My best code is written with the delete key.

  5. #5
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    ...and a third use of static is within a class; to specify that only one function/variable exists for all instances (and can be accessed/used when no individual object has been created).

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Also, a static variable is only initialized ONCE. So if you had:

    int foo()
    {
    static int i = 0; ///...only set to zero first call..

    i++;

    return i;
    }


    ...call the function in a loop and it will print: 1, 2, 3, 4, 5, 6...etc.

    whereas

    int foo()
    {
    int i = 0;

    i++;

    return i;
    }


    ...call THIS function in a loop and it will print: 1, 1, 1, 1, 1, ...etc.


    There are many uses for static variables referenced in this way...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    20

    Angry

    What I can't understand is why nobody seems to think there is anything bizarre about a language that has to use the same word to mean so many different things. Static is a fine name for any one of its 4 different meanings, but is there no limit?

    Ok, I can go along with static member variables and static nonmember variables, but can anybody tell me why the global function thing needs to be called static. And BTW, how did static_cast get in there?

    I think I'm going to start a Pascal society Al

  8. #8
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    What I can't understand is why nobody seems to think there is anything bizarre about a language that has to use the same word to mean so many different things.
    People do think it's bizarre, which is why unnamed namespaces were invented; to prevent the need for one of its uses. But nobody seems to use them, and just tend to stick with static.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What I can't understand is why nobody seems to think there is anything bizarre about a language that has to use the same word to mean so many different things. Static is a fine name for any one of its 4 different meanings, but is there no limit?
    Everyone thinks it's bizarre for the same keyword to have wildly different meanings based on context. If you find out why it's like this, let us know.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. uploading file to http server via multipart form data
    By Dynamo in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2008, 04:36 AM
  4. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 05:05 PM
  5. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM