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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. static variable

static variable

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
39 Posts 8 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.
  • G Offline
    G Offline
    George_George
    wrote on last edited by
    #1

    Hello everyone, I am wondering how C or C++ manages static variable internally. Since each time when we again a function again, if in this function, a static variable is defined, the value will be the value last time when we entered this function (i.e. will not be initialized again, and only initialized at the 1st time). I suspect it is stored in some global structure to reserve the value? thanks in advance, George

    L B D G 4 Replies Last reply
    0
    • G George_George

      Hello everyone, I am wondering how C or C++ manages static variable internally. Since each time when we again a function again, if in this function, a static variable is defined, the value will be the value last time when we entered this function (i.e. will not be initialized again, and only initialized at the 1st time). I suspect it is stored in some global structure to reserve the value? thanks in advance, George

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      My understanding is that static vars are initialised before main() is called. You could check this by creating a class with a constructor, init a static variable of this type, put a breakpoint in the constructor and see what happens

      G 1 Reply Last reply
      0
      • L Lost User

        My understanding is that static vars are initialised before main() is called. You could check this by creating a class with a constructor, init a static variable of this type, put a breakpoint in the constructor and see what happens

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #3

        Thanks Josh, Do you mean static variable inside a function? If yes, how do you think such *local* static variable will be stored, since normal local variable (value) will vanish after function returns. But for static *local* variable, the value will not vanish -- and will be recorded next time when we entered the same function. Any ideas or comments? regards, George

        L 1 Reply Last reply
        0
        • G George_George

          Thanks Josh, Do you mean static variable inside a function? If yes, how do you think such *local* static variable will be stored, since normal local variable (value) will vanish after function returns. But for static *local* variable, the value will not vanish -- and will be recorded next time when we entered the same function. Any ideas or comments? regards, George

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          I dont know to be honest but I would imagine that all static variables are pushed onto the stack before main() is called and therefore "local" statics dont really go out of scope when a function ends. I suspect the actual details may vary between compilers.

          G 1 Reply Last reply
          0
          • L Lost User

            I dont know to be honest but I would imagine that all static variables are pushed onto the stack before main() is called and therefore "local" statics dont really go out of scope when a function ends. I suspect the actual details may vary between compilers.

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #5

            Thanks Josh, Good answer! I have a further question about programming best practice. Do you think it is safe to let the address of the *local* static variable as the return value of a function? Then other part of code (out of this function) will access or even modify the variable by the returned address of the *local* static variable? Any disadvantages of this approach? Example, int* func() { static int i; // other code return &i; } regards, George

            T L 2 Replies Last reply
            0
            • G George_George

              Thanks Josh, Good answer! I have a further question about programming best practice. Do you think it is safe to let the address of the *local* static variable as the return value of a function? Then other part of code (out of this function) will access or even modify the variable by the returned address of the *local* static variable? Any disadvantages of this approach? Example, int* func() { static int i; // other code return &i; } regards, George

              T Offline
              T Offline
              ThatsAlok
              wrote on last edited by
              #6

              when you mark some variable static, compiler wil decide how to treat that variable.. so there no scope of local and global variable concept here!

              "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
              Never mind - my own stupidity is the source of every "problem" - Mixture

              cheers, Alok Gupta VC Forum Q&A :- I IV Support CRY- Child Relief and You

              G 1 Reply Last reply
              0
              • G George_George

                Thanks Josh, Good answer! I have a further question about programming best practice. Do you think it is safe to let the address of the *local* static variable as the return value of a function? Then other part of code (out of this function) will access or even modify the variable by the returned address of the *local* static variable? Any disadvantages of this approach? Example, int* func() { static int i; // other code return &i; } regards, George

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                George_George wrote:

                Good answer! I have a further question about programming best practice. Do you think it is safe to let the address of the *local* static variable as the return value of a function? Then other part of code (out of this function) will access or even modify the variable by the returned address of the *local* static variable? Any disadvantages of this approach?

                I'd say it smells like bad design, the only reason to return a pointer rather than a copy of the int is to allow some other code to modify it. I'd rather provide a method to allow other code to modify the static.

                T G 2 Replies Last reply
                0
                • L Lost User

                  George_George wrote:

                  Good answer! I have a further question about programming best practice. Do you think it is safe to let the address of the *local* static variable as the return value of a function? Then other part of code (out of this function) will access or even modify the variable by the returned address of the *local* static variable? Any disadvantages of this approach?

                  I'd say it smells like bad design, the only reason to return a pointer rather than a copy of the int is to allow some other code to modify it. I'd rather provide a method to allow other code to modify the static.

                  T Offline
                  T Offline
                  ThatsAlok
                  wrote on last edited by
                  #8

                  Josh Gray wrote:

                  I'd say it smells like bad design, the only reason to return a pointer rather than a copy of the int is to allow some other code to modify it. I'd rather provide a method to allow other code to modify the static.

                  sorry to interrupt you.. it's somewhat Singleton pattern...! so i don't think it bad design.. though it break OOPS basic concept of encapsulation and abstraction!

                  "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                  Never mind - my own stupidity is the source of every "problem" - Mixture

                  cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                  L G 2 Replies Last reply
                  0
                  • T ThatsAlok

                    Josh Gray wrote:

                    I'd say it smells like bad design, the only reason to return a pointer rather than a copy of the int is to allow some other code to modify it. I'd rather provide a method to allow other code to modify the static.

                    sorry to interrupt you.. it's somewhat Singleton pattern...! so i don't think it bad design.. though it break OOPS basic concept of encapsulation and abstraction!

                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                    Never mind - my own stupidity is the source of every "problem" - Mixture

                    cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    ThatsAlok wrote:

                    i don't think it bad design.. though it break OOPS basic concept of encapsulation and abstraction!

                    Which to me makes it bad design. You can still have the static and therefore the singleton pattern but my comments was more about how you expose the interface to it. Returning the address of what is basically a private variable is bad.

                    G 1 Reply Last reply
                    0
                    • L Lost User

                      George_George wrote:

                      Good answer! I have a further question about programming best practice. Do you think it is safe to let the address of the *local* static variable as the return value of a function? Then other part of code (out of this function) will access or even modify the variable by the returned address of the *local* static variable? Any disadvantages of this approach?

                      I'd say it smells like bad design, the only reason to return a pointer rather than a copy of the int is to allow some other code to modify it. I'd rather provide a method to allow other code to modify the static.

                      G Offline
                      G Offline
                      George_George
                      wrote on last edited by
                      #10

                      Thanks Josh,

                      Josh Gray wrote:

                      I'd rather provide a method to allow other code to modify the static.

                      I agree with most points of you. But I do not quite understand your above points. Could you show me a sample please about how to modify the static variable please? I am wondering how to modify the value of the static variable from other function (other than from the function in which it is defined)? regards, George

                      L 1 Reply Last reply
                      0
                      • L Lost User

                        ThatsAlok wrote:

                        i don't think it bad design.. though it break OOPS basic concept of encapsulation and abstraction!

                        Which to me makes it bad design. You can still have the static and therefore the singleton pattern but my comments was more about how you expose the interface to it. Returning the address of what is basically a private variable is bad.

                        G Offline
                        G Offline
                        George_George
                        wrote on last edited by
                        #11

                        Thanks Josh, I can generally understand and agree with your points. But I am confused about some details. Could you show us your points by a couple of lines of code please? regards, George

                        1 Reply Last reply
                        0
                        • T ThatsAlok

                          Josh Gray wrote:

                          I'd say it smells like bad design, the only reason to return a pointer rather than a copy of the int is to allow some other code to modify it. I'd rather provide a method to allow other code to modify the static.

                          sorry to interrupt you.. it's somewhat Singleton pattern...! so i don't think it bad design.. though it break OOPS basic concept of encapsulation and abstraction!

                          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                          Never mind - my own stupidity is the source of every "problem" - Mixture

                          cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                          G Offline
                          G Offline
                          George_George
                          wrote on last edited by
                          #12

                          Thanks Alok! How do you think returning static variable is similar to Singleton pattern? Any more details about how similar points do they have? regards, George

                          T 1 Reply Last reply
                          0
                          • T ThatsAlok

                            when you mark some variable static, compiler wil decide how to treat that variable.. so there no scope of local and global variable concept here!

                            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                            Never mind - my own stupidity is the source of every "problem" - Mixture

                            cheers, Alok Gupta VC Forum Q&A :- I IV Support CRY- Child Relief and You

                            G Offline
                            G Offline
                            George_George
                            wrote on last edited by
                            #13

                            Thanks Alok! If you think static variable is different from local or global variables, how do you think static variable is stored and managed so that the value is reserved each time we enters the function in which the static variable is defined? regards, George

                            1 Reply Last reply
                            0
                            • G George_George

                              Thanks Josh,

                              Josh Gray wrote:

                              I'd rather provide a method to allow other code to modify the static.

                              I agree with most points of you. But I do not quite understand your above points. Could you show me a sample please about how to modify the static variable please? I am wondering how to modify the value of the static variable from other function (other than from the function in which it is defined)? regards, George

                              L Offline
                              L Offline
                              Lost User
                              wrote on last edited by
                              #14

                              George_George wrote:

                              I am wondering how to modify the value of the static variable from other function (other than from the function in which it is defined)?

                              Sorry, my mistake as you cant do that. You can however define a static in a class and access it from any of the methods in that class. Even const methods!

                              G 1 Reply Last reply
                              0
                              • L Lost User

                                George_George wrote:

                                I am wondering how to modify the value of the static variable from other function (other than from the function in which it is defined)?

                                Sorry, my mistake as you cant do that. You can however define a static in a class and access it from any of the methods in that class. Even const methods!

                                G Offline
                                G Offline
                                George_George
                                wrote on last edited by
                                #15

                                Hi Josh, I think you mean static member of a class. But I mean a static variable defined inside a function. regards, George

                                L 1 Reply Last reply
                                0
                                • G George_George

                                  Hi Josh, I think you mean static member of a class. But I mean a static variable defined inside a function. regards, George

                                  L Offline
                                  L Offline
                                  Lost User
                                  wrote on last edited by
                                  #16

                                  George_George wrote:

                                  I think you mean static member of a class. But I mean a static variable defined inside a function.

                                  Yes you are right which makes me wonder why you have global methods? I suspect there is probably a better to solution to what you are trying to do but its hard to suggest anything without knowing more about what you are doing and in what context

                                  G 1 Reply Last reply
                                  0
                                  • G George_George

                                    Thanks Alok! How do you think returning static variable is similar to Singleton pattern? Any more details about how similar points do they have? regards, George

                                    T Offline
                                    T Offline
                                    ThatsAlok
                                    wrote on last edited by
                                    #17

                                    George_George wrote:

                                    How do you think returning static variable is similar to Singleton pattern

                                    just on basic.. concept of Singleton pattern says that.. there should only copy exist for any variable.. look more in detail here:- http://www.codeproject.com/cpp/singletonrvs.asp[^]

                                    "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                                    Never mind - my own stupidity is the source of every "problem" - Mixture

                                    cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                                    G 1 Reply Last reply
                                    0
                                    • L Lost User

                                      George_George wrote:

                                      I think you mean static member of a class. But I mean a static variable defined inside a function.

                                      Yes you are right which makes me wonder why you have global methods? I suspect there is probably a better to solution to what you are trying to do but its hard to suggest anything without knowing more about what you are doing and in what context

                                      G Offline
                                      G Offline
                                      George_George
                                      wrote on last edited by
                                      #18

                                      Thanks Josh, You are right. I am writing a part of the application in C (not C++). I am wondering and willing to listen to your comments and ideas of any disadvantages if I return the address of static variable defined in a function, then let other part of code to access the variable by the returned address. regards, George

                                      L 1 Reply Last reply
                                      0
                                      • G George_George

                                        Thanks Josh, You are right. I am writing a part of the application in C (not C++). I am wondering and willing to listen to your comments and ideas of any disadvantages if I return the address of static variable defined in a function, then let other part of code to access the variable by the returned address. regards, George

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #19

                                        George_George wrote:

                                        You are right. I am writing a part of the application in C (not C++). I am wondering and willing to listen to your comments and ideas of any disadvantages if I return the address of static variable defined in a function, then let other part of code to access the variable by the returned address.

                                        Well you could make the static global rather than defining it within a function then have two function to get and set its value but thats just an attempt to make a procedural language more OO.

                                        G 1 Reply Last reply
                                        0
                                        • T ThatsAlok

                                          George_George wrote:

                                          How do you think returning static variable is similar to Singleton pattern

                                          just on basic.. concept of Singleton pattern says that.. there should only copy exist for any variable.. look more in detail here:- http://www.codeproject.com/cpp/singletonrvs.asp[^]

                                          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
                                          Never mind - my own stupidity is the source of every "problem" - Mixture

                                          cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                                          G Offline
                                          G Offline
                                          George_George
                                          wrote on last edited by
                                          #20

                                          Thanks Alok, The similarity you mean is only one copy of data? regards, George

                                          T 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