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. Storage for Statis Data members

Storage for Statis Data members

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

    Hi All, I want to know that when & where does the storage for static data members of a class is defined and what is it's life time. Since we don't need to create an object for assessing static members, does the storage is defined only by defining a class with static data members. Please let me know about the same ASAP. Thanks, Akash

    CPalliniC L S 3 Replies Last reply
    0
    • A AkashAg

      Hi All, I want to know that when & where does the storage for static data members of a class is defined and what is it's life time. Since we don't need to create an object for assessing static members, does the storage is defined only by defining a class with static data members. Please let me know about the same ASAP. Thanks, Akash

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      akashag wrote:

      Since we don't need to create an object for assessing static members, does the storage is defined only by defining a class with static data members.

      You've to explicitely define the static members of a class, once you've declared them inside the class.

      akashag wrote:

      Please let me know about the same ASAP.

      Fast enough? :-D

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • A AkashAg

        Hi All, I want to know that when & where does the storage for static data members of a class is defined and what is it's life time. Since we don't need to create an object for assessing static members, does the storage is defined only by defining a class with static data members. Please let me know about the same ASAP. Thanks, Akash

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

        akashag wrote:

        storage for static data members of a class

        Since static members have to be available whether an object of the class exists or not, they are allocated in fixed program memory by the compiler. Thus they will be present during the lifetime of the program.

        A 1 Reply Last reply
        0
        • A AkashAg

          Hi All, I want to know that when & where does the storage for static data members of a class is defined and what is it's life time. Since we don't need to create an object for assessing static members, does the storage is defined only by defining a class with static data members. Please let me know about the same ASAP. Thanks, Akash

          S Offline
          S Offline
          santhosh padamatinti
          wrote on last edited by
          #4

          akashag wrote:

          I want to know that when & where does the storage for static data members of a class is defined and what is it's life time.

          Hi Akash, Static data members are having global scope comparing to local variables. The simple example for this is when ever you are trying to create a static variable inside a class it gives an error. That is the memory is allocated to class variables at run time. Where as memory allocation done at compile time in case of static variables. So when ever i am creating variable inside a class means I giving memory at run time, both the things are contradiction. A variable for that memory allocation done at only once called static variable.

          sampath-padamatinti

          L 1 Reply Last reply
          0
          • L Lost User

            akashag wrote:

            storage for static data members of a class

            Since static members have to be available whether an object of the class exists or not, they are allocated in fixed program memory by the compiler. Thus they will be present during the lifetime of the program.

            A Offline
            A Offline
            AkashAg
            wrote on last edited by
            #5

            Hi, Thanks for your answer. Between, I assume that by 'fixed program memory', you mean heap. Isn't it? Secondly, at what time storage for them is reserved? Rgrds, Akash

            CPalliniC L 2 Replies Last reply
            0
            • A AkashAg

              Hi, Thanks for your answer. Between, I assume that by 'fixed program memory', you mean heap. Isn't it? Secondly, at what time storage for them is reserved? Rgrds, Akash

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              akashag wrote:

              etween, I assume that by 'fixed program memory', you mean heap

              Why?

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              • A AkashAg

                Hi, Thanks for your answer. Between, I assume that by 'fixed program memory', you mean heap. Isn't it? Secondly, at what time storage for them is reserved? Rgrds, Akash

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

                akashag wrote:

                Between, I assume that by 'fixed program memory', you mean heap. Isn't it?

                No, I mean fixed program memory.

                akashag wrote:

                Secondly, at what time storage for them is reserved?

                As I said before, at compile time. Static data whether global or class is allocated by the compiler, it is not dynamic (i.e. run time allocated).

                1 Reply Last reply
                0
                • S santhosh padamatinti

                  akashag wrote:

                  I want to know that when & where does the storage for static data members of a class is defined and what is it's life time.

                  Hi Akash, Static data members are having global scope comparing to local variables. The simple example for this is when ever you are trying to create a static variable inside a class it gives an error. That is the memory is allocated to class variables at run time. Where as memory allocation done at compile time in case of static variables. So when ever i am creating variable inside a class means I giving memory at run time, both the things are contradiction. A variable for that memory allocation done at only once called static variable.

                  sampath-padamatinti

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

                  sampath-padamatinti wrote:

                  The simple example for this is when ever you are trying to create a static variable inside a class it gives an error.

                  What does this mean? It is quite correct to have static variables in a class: they are known as class variables.

                  sampath-padamatinti wrote:

                  That is the memory is allocated to class variables at run time.

                  No, class variables are allocated at compile time.

                  sampath-padamatinti wrote:

                  both the things are contradiction.

                  :confused:

                  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