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. How Static variable persists/retains its value.?

How Static variable persists/retains its value.?

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
6 Posts 3 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.
  • M Offline
    M Offline
    mbatra31
    wrote on last edited by
    #1

    Hi, I need some clarification on one point. We normally use static variables in C/C++. We say when we have used a static variable inside a function, it persists its previous value when that function is called again & again, e:g: to say, static variable retains its last value when that function is called again. Can anybody explaing me the logic behind this. How a static variable knows its last value and how it is not initialized everytime on the function call. Regards, Mbatra

    P L 2 Replies Last reply
    0
    • M mbatra31

      Hi, I need some clarification on one point. We normally use static variables in C/C++. We say when we have used a static variable inside a function, it persists its previous value when that function is called again & again, e:g: to say, static variable retains its last value when that function is called again. Can anybody explaing me the logic behind this. How a static variable knows its last value and how it is not initialized everytime on the function call. Regards, Mbatra

      P Offline
      P Offline
      PecuniousPete
      wrote on last edited by
      #2

      Because a static variable, even when declared in side a func, is created in the DATA section of a programs run space. Think of it like a global static variable that gets initialised to zero and whose value is of course persistent. An ordinary variable declared inside a func is created on the stack. This of course winds back and forwards so when the func returns the space that variable used is lost to the next func you call.

      1 Reply Last reply
      0
      • M mbatra31

        Hi, I need some clarification on one point. We normally use static variables in C/C++. We say when we have used a static variable inside a function, it persists its previous value when that function is called again & again, e:g: to say, static variable retains its last value when that function is called again. Can anybody explaing me the logic behind this. How a static variable knows its last value and how it is not initialized everytime on the function call. Regards, Mbatra

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

        Read the discussion. refer the link below. http://stackoverflow.com/questions/898432/how-is-static-variable-initialization-implemented-by-the-compiler

        P 1 Reply Last reply
        0
        • L Lost User

          Read the discussion. refer the link below. http://stackoverflow.com/questions/898432/how-is-static-variable-initialization-implemented-by-the-compiler

          P Offline
          P Offline
          PecuniousPete
          wrote on last edited by
          #4

          Stackoverflow? The competition? Are you mad? :)

          L 1 Reply Last reply
          0
          • P PecuniousPete

            Stackoverflow? The competition? Are you mad? :)

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

            Please read that. carefully. ok.. :)

            P 1 Reply Last reply
            0
            • L Lost User

              Please read that. carefully. ok.. :)

              P Offline
              P Offline
              PecuniousPete
              wrote on last edited by
              #6

              Why? Are you saying I am wrong? :)

              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