Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Could anyone expain the "Static" to me

Could anyone expain the "Static" to me

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bloodwinner
    wrote on last edited by
    #1

    I checked msdn, and got this "Objects and variables declared as static retain their values for the duration of the program’s execution".... but still don't know what is exactly the difference between declaring "int a" and "static int a", what is the difference when using them?

    L T H 3 Replies Last reply
    0
    • B bloodwinner

      I checked msdn, and got this "Objects and variables declared as static retain their values for the duration of the program’s execution".... but still don't know what is exactly the difference between declaring "int a" and "static int a", what is the difference when using them?

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #2

      One difference is that a static variable is assigned a memory address before the applications code executes. A local variable is not pushed onto the stack until that function is entered.

      led mike

      1 Reply Last reply
      0
      • B bloodwinner

        I checked msdn, and got this "Objects and variables declared as static retain their values for the duration of the program’s execution".... but still don't know what is exactly the difference between declaring "int a" and "static int a", what is the difference when using them?

        T Offline
        T Offline
        toxcct
        wrote on last edited by
        #3

        int i your variable will be created when the execution reaches this line, and it will be destroyed when leaving the scope. static int i your variable will be created when the program starts (whereever the statement is placed) and will be destroyed when the program exits.


        TOXCCT >>> GEII power

        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

        B A 2 Replies Last reply
        0
        • T toxcct

          int i your variable will be created when the execution reaches this line, and it will be destroyed when leaving the scope. static int i your variable will be created when the program starts (whereever the statement is placed) and will be destroyed when the program exits.


          TOXCCT >>> GEII power

          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

          B Offline
          B Offline
          bloodwinner
          wrote on last edited by
          #4

          again, I am still confusing... is that int i: it will be ready when you start use it static int i: it will be ready long before you use it So what's the use of the latter occasion? Since I don't use it, why should I care when it is created.

          T L 2 Replies Last reply
          0
          • B bloodwinner

            again, I am still confusing... is that int i: it will be ready when you start use it static int i: it will be ready long before you use it So what's the use of the latter occasion? Since I don't use it, why should I care when it is created.

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            static has another meaning. when you declare a variable local to a function as static, then is keeps its last value :

            void foo() {
            static int i = 0;
            i++;
            printf("%d\n", i);
            }

            void main() {
            for (int n = 0; n < 5; n++) {
            foo();
            }
            }

            this prints : 1 2 3 4 5 another meaning is when you declare a class data member as static. this means that the member is shared between every instances of the class. this is useful when you want a member to be unique, like an instances counter, class constants, etc... the last meaning is when declaring a class member function as static. it will tell that the function is a class function ; it doesn't know a particuliar instance (no this pointer), it performs general operations...


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            1 Reply Last reply
            0
            • B bloodwinner

              again, I am still confusing... is that int i: it will be ready when you start use it static int i: it will be ready long before you use it So what's the use of the latter occasion? Since I don't use it, why should I care when it is created.

              L Offline
              L Offline
              Lilith C
              wrote on last edited by
              #6

              bloodwinner wrote:

              again, I am still confusing... is that int i: it will be ready when you start use it static int i: it will be ready long before you use it So what's the use of the latter occasion? Since I don't use it, why should I care when it is created.

              Imagine a function that animates something on the screen. Each call to the function updates the phase of the animation by using a counter to determine what's displayed. To this end you need an int as the counter and you declare it inside the function rather than having your primary program pass the counter to the function. If you declare the int as non-static but you increment the int each time you enter the function the value of the int goes away because the variable was created on the stack when the function began and it was removed when the function ended. If you declare the int as static, whatever value it had on exit from the function is the value it will have when you call the function again because the variable was created before the program began execution and will not be destroyed until the program ends. Lilith

              1 Reply Last reply
              0
              • T toxcct

                int i your variable will be created when the execution reaches this line, and it will be destroyed when leaving the scope. static int i your variable will be created when the program starts (whereever the statement is placed) and will be destroyed when the program exits.


                TOXCCT >>> GEII power

                [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                A Offline
                A Offline
                Akt_4_U
                wrote on last edited by
                #7

                A Static variable has a global scope. So if the value of static variable changed, that change will occur where ever the static variable is used.

                Akkott

                1 Reply Last reply
                0
                • B bloodwinner

                  I checked msdn, and got this "Objects and variables declared as static retain their values for the duration of the program’s execution".... but still don't know what is exactly the difference between declaring "int a" and "static int a", what is the difference when using them?

                  H Offline
                  H Offline
                  Hamid Taebi
                  wrote on last edited by
                  #8

                  See static Methods and Variables [^] and here[^]

                  _**


                  **_

                  WhiteSky


                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups