something interesting on static~

This is a discussion on something interesting on static~ within the C++ Programming forums, part of the General Programming Boards category; Hi, folks~ Let's see the function below first: PHP Code: void sta () {   static  int a ;    a = 0 ...

  1. #1
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563

    Smile something interesting on static~

    Hi, folks~

    Let's see the function below first:

    PHP Code:
    void sta()
    {
      static 
    int a;
      
    a=0;
      
    a++;
      
    cout << "static a= " << << endl;

    No doubt we cuold get the value 0 all the time.
    and then the next function:

    PHP Code:
    void sta()
    {
      static 
    int a=0;
      
    a++;
      
    cout << "static a= " << << endl;

    Please Check the difference, a bit interesting~
    Never end on learning~

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    >a=0;

    this is assignment.

    >static..int..a=0;

    this is initialization.

    the difference is that static variables are only initialized once, where-as they can be assigned to unlimited times.
    ADVISORY: This users posts are rated CP-MA, for Mature Audiences only.

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    No-one is right. (Just went over this one today as a matter of fact.)

    I believe Mr. Stroustrup refers to a static variable as one "with memory", i.e. it isn't destroyed at the end of scope.

    From what I could discern, it can be "played wiith" within its scope (not outside), but will retain its most recent value.

    Thanks, Black. Good stuff. (And, thanks, no-one, for explaining it.)

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    yep. From the input we could include static acts just like somewhat global inside functions. It initialize only once, and it ignores the defination when was calling again, just the following code executes.

    thanx no-one and skipper, your explaination make it clear now~
    Never end on learning~

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >a bit interesting
    Another interesting fact about static variables with local scope is that they are automatically initialized to zero, just like global variables.

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

  6. #6
    flashing vampire black's Avatar
    Join Date
    May 2002
    Posts
    563
    Originally posted by Prelude
    >a bit interesting
    Another interesting fact about static variables with local scope is that they are automatically initialized to zero, just like global variables.

    -Prelude
    oh, yes it is ! fantastic~

    thanx Prelude for mention this.
    Never end on learning~

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    Another interesting fact about static variables with local scope is that they are automatically initialized to zero, just like global variables.
    And - are we getting some mileage out of this, or what? - once an initializing statement, if any, is encountered, it is ignored afterward.

    That is, a statement such as
    Code:
    static int x = 0;
    found within a loop structure does not reset the current value of 'x' to '0'. (The little devil takes on a life of its own, it seems. )

    Thanks, Prelude.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A few interesting tools...
    By Mad_guy in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-10-2009, 06:07 PM
  2. interesting internet statistics
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 9
    Last Post: 06-24-2004, 06:17 AM
  3. Interesting number theory problem
    By Zach L. in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 09-20-2003, 07:45 AM
  4. very interesting.....VERY interesting
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 08-16-2002, 10:02 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21