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. Question about static variable that define in a function

Question about static variable that define in a function

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
4 Posts 2 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.
  • N Offline
    N Offline
    nachilau
    wrote on last edited by
    #1

    Hello all, Would anyone tell me that how C++ prevent the static variable been created more than one? Eg, if I have a function that contains a static variable: void f() { static classA a; a.print(); } void main() { f(); f(); } How C++ prevents the static variable "a" inside the function to be constructed twice rather than once? If I inline the function f(), will it still works? Thanks!!!:) Nacho

    J 1 Reply Last reply
    0
    • N nachilau

      Hello all, Would anyone tell me that how C++ prevent the static variable been created more than one? Eg, if I have a function that contains a static variable: void f() { static classA a; a.print(); } void main() { f(); f(); } How C++ prevents the static variable "a" inside the function to be constructed twice rather than once? If I inline the function f(), will it still works? Thanks!!!:) Nacho

      J Offline
      J Offline
      Joaquin M Lopez Munoz
      wrote on last edited by
      #2

      Hi Nacho, The static variable will get created only once (exactly, the first time f is invoked.) Are you observing something different? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

      N 1 Reply Last reply
      0
      • J Joaquin M Lopez Munoz

        Hi Nacho, The static variable will get created only once (exactly, the first time f is invoked.) Are you observing something different? Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

        N Offline
        N Offline
        nachilau
        wrote on last edited by
        #3

        Hello, Yes, I know that the static will created only once. But I want to know how C++ actualy implement this. And I want to know whether doing inline of f() will break the rule? Thanks! Nacho:)

        J 1 Reply Last reply
        0
        • N nachilau

          Hello, Yes, I know that the static will created only once. But I want to know how C++ actualy implement this. And I want to know whether doing inline of f() will break the rule? Thanks! Nacho:)

          J Offline
          J Offline
          Joaquin M Lopez Munoz
          wrote on last edited by
          #4

          But I want to know how C++ actualy implement this, Well, how the compiler implements this feature is up to the compiler alone, I guess. You can assume the internally generated code will resemble in some way the following:

          static bool __f_a_initialized=false;
          static char __f_a_storage[sizeof(classA)];

          void f()
          {
          if(!__f_a_initialized){
          new (__f_a_storage) class A; // constructs a classA onto __f_a_storage
          __f_a_initialized=true;
          }

          ...
          }

          Do not take this as the actual procedure implemented, it is just an approximation to what the compiler probably does. Does this answer your question? And I want to know whether doing inline of f() will break the rule? The rule does not break even if you declare your function inline. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo Want a Boost forum in Code Project? Vote here[^]!

          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