static allocation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sks_cpp

    static allocation

    When do static members allocate memory? (at load time)

    How about static variables within a method?

    Where is the memory for static variables - is it allocated on the heap or
    does it have a region of its own?


  • John Harrison

    #2
    Re: static allocation


    "sks_cpp" <sksjava@hotmai l.com> wrote in message
    news:hLHVa.1057 78$R92.24264@ne ws2.central.cox .net...[color=blue]
    > When do static members allocate memory? (at load time)
    >
    > How about static variables within a method?
    >
    > Where is the memory for static variables - is it allocated on the heap or
    > does it have a region of its own?
    >[/color]

    All static variables, of all types, have their memory allocated at load
    time.

    john


    Comment

    • Josephine Schafer

      #3
      Re: static allocation


      "John Harrison" <john_andronicu s@hotmail.com> wrote in message
      news:bg7hqi$lhg 80$1@ID-196037.news.uni-berlin.de...[color=blue]
      >
      > "sks_cpp" <sksjava@hotmai l.com> wrote in message
      > news:hLHVa.1057 78$R92.24264@ne ws2.central.cox .net...[color=green]
      > > When do static members allocate memory? (at load time)
      > >
      > > How about static variables within a method?
      > >
      > > Where is the memory for static variables - is it allocated on the heap[/color][/color]
      or[color=blue][color=green]
      > > does it have a region of its own?
      > >[/color]
      >
      > All static variables, of all types, have their memory allocated at load
      > time.[/color]

      Static variables in a function are constructed and destroyed only on the
      first invocation of the function.
      If the function never gets called the object is never created. Ofcourse this
      means an extra overhead to check if the
      object needs to be created each time the function is invoked.
      But don't pay for things you don't use.

      --
      With best wishes,
      J.Schafer


      Comment

      • Josephine Schafer

        #4
        Re: static allocation


        [color=blue]
        >
        > Where is the memory for static variables - is it allocated on the heap or
        > does it have a region of its own?[/color]
        Static variables are not allocated on the heap.
        Yes they have a region of their own.


        Comment

        • John Harrison

          #5
          Re: static allocation


          "Josephine Schafer" <jsf@usa.net> wrote in message
          news:bg7ip6$l0h te$1@ID-192448.news.uni-berlin.de...[color=blue]
          >
          > "John Harrison" <john_andronicu s@hotmail.com> wrote in message
          > news:bg7hqi$lhg 80$1@ID-196037.news.uni-berlin.de...[color=green]
          > >
          > > "sks_cpp" <sksjava@hotmai l.com> wrote in message
          > > news:hLHVa.1057 78$R92.24264@ne ws2.central.cox .net...[color=darkred]
          > > > When do static members allocate memory? (at load time)
          > > >
          > > > How about static variables within a method?
          > > >
          > > > Where is the memory for static variables - is it allocated on the heap[/color][/color]
          > or[color=green][color=darkred]
          > > > does it have a region of its own?
          > > >[/color]
          > >
          > > All static variables, of all types, have their memory allocated at load
          > > time.[/color]
          >
          > Static variables in a function are constructed and destroyed only on the
          > first invocation of the function.
          > If the function never gets called the object is never created. Ofcourse[/color]
          this[color=blue]
          > means an extra overhead to check if the
          > object needs to be created each time the function is invoked.
          > But don't pay for things you don't use.
          >[/color]

          The question wsn't about construction, it was about memory allocation.

          john


          Comment

          • Josephine Schafer

            #6
            Re: static allocation


            "John Harrison" <john_andronicu s@hotmail.com> wrote in message
            news:bg7jkm$lje jo$1@ID-196037.news.uni-berlin.de...[color=blue]
            >
            > "Josephine Schafer" <jsf@usa.net> wrote in message
            > news:bg7ip6$l0h te$1@ID-192448.news.uni-berlin.de...[color=green]
            > >
            > > "John Harrison" <john_andronicu s@hotmail.com> wrote in message
            > > news:bg7hqi$lhg 80$1@ID-196037.news.uni-berlin.de...[color=darkred]
            > > >
            > > > "sks_cpp" <sksjava@hotmai l.com> wrote in message
            > > > news:hLHVa.1057 78$R92.24264@ne ws2.central.cox .net...
            > > > > When do static members allocate memory? (at load time)
            > > > >
            > > > > How about static variables within a method?
            > > > >
            > > > > Where is the memory for static variables - is it allocated on the[/color][/color][/color]
            heap[color=blue][color=green]
            > > or[color=darkred]
            > > > > does it have a region of its own?
            > > > >
            > > >
            > > > All static variables, of all types, have their memory allocated at[/color][/color][/color]
            load[color=blue][color=green][color=darkred]
            > > > time.[/color]
            > >
            > > Static variables in a function are constructed and destroyed only on the
            > > first invocation of the function.
            > > If the function never gets called the object is never created. Ofcourse[/color]
            > this[color=green]
            > > means an extra overhead to check if the
            > > object needs to be created each time the function is invoked.
            > > But don't pay for things you don't use.
            > >[/color]
            >
            > The question wsn't about construction, it was about memory allocation.
            >[/color]

            See static objects in a class are always constructed and destructed, no
            matter whether they are ever used or not.
            The same is not true for static variables in a function. So shouldn't the
            implementation defer the memory allocation also
            for such variables till their first invocation just like construction?
            Else we have paid a little even without having used it :-).





            Comment

            • Greg Comeau

              #7
              Re: static allocation

              In article <bg7je1$loq11$1 @ID-192448.news.uni-berlin.de>,
              Josephine Schafer <jsf@usa.net> wrote:[color=blue][color=green]
              >> Where is the memory for static variables - is it allocated on the heap or
              >> does it have a region of its own?[/color]
              >Static variables are not allocated on the heap.
              >Yes they have a region of their own.[/color]

              Just a side comment here that the standard never discusses
              things such as the stack, etc. So these are normally
              implementation things, though most implementation tend to
              use the same stragegy (optimization issues aside).
              --
              Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
              Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
              World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
              Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

              Comment

              • Greg Comeau

                #8
                Re: static allocation

                In article <bg7hqi$lhg80$1 @ID-196037.news.uni-berlin.de>,
                John Harrison <john_andronicu s@hotmail.com> wrote:[color=blue]
                >"sks_cpp" <sksjava@hotmai l.com> wrote in message
                >news:hLHVa.105 778$R92.24264@n ews2.central.co x.net...[color=green]
                >> When do static members allocate memory? (at load time)
                >>
                >> How about static variables within a method?
                >>
                >> Where is the memory for static variables - is it allocated on the heap or
                >> does it have a region of its own?[/color]
                >
                >All static variables, of all types, have their memory allocated at load
                >time.[/color]

                Well, many compilers do it that way for namespace'd statics at least.
                Of course too, this does not necessarily apply to static members
                and probably some other situations too.
                --
                Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
                Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
                World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                Comment

                • Greg Comeau

                  #9
                  Re: static allocation

                  In article <bg7ip6$l0hte$1 @ID-192448.news.uni-berlin.de>,
                  Josephine Schafer <jsf@usa.net> wrote:[color=blue]
                  >"John Harrison" <john_andronicu s@hotmail.com> wrote in message
                  >news:bg7hqi$lh g80$1@ID-196037.news.uni-berlin.de...[color=green]
                  >> "sks_cpp" <sksjava@hotmai l.com> wrote in message
                  >> news:hLHVa.1057 78$R92.24264@ne ws2.central.cox .net...[color=darkred]
                  >> > When do static members allocate memory? (at load time)
                  >> >
                  >> > How about static variables within a method?
                  >> >
                  >> > Where is the memory for static variables - is it allocated
                  >> > on the heap or
                  >> > does it have a region of its own?[/color]
                  >>
                  >> All static variables, of all types, have their memory
                  >> allocated at load time.[/color]
                  >
                  >Static variables in a function are constructed and destroyed only on the
                  >first invocation of the function.[/color]

                  No, this too is just one of many possibilities.
                  For instance, it may be done elsewhere. Or, it may be repeated
                  if "EH failed".
                  [color=blue]
                  >If the function never gets called the object is never created.[/color]

                  Perhaps.
                  [color=blue]
                  > Ofcourse this
                  >means an extra overhead to check if the
                  >object needs to be created each time the function is invoked.
                  >But don't pay for things you don't use.[/color]

                  In a traditional implementation.
                  --
                  Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
                  Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
                  World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                  Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                  Comment

                  • Greg Comeau

                    #10
                    Re: static allocation

                    In article <bg8110$ktb28$1 @ID-192448.news.uni-berlin.de>,
                    Josephine Schafer <jsf@usa.net> wrote:[color=blue]
                    >See static objects in a class are always constructed and destructed, no
                    >matter whether they are ever used or not.[/color]

                    But that's different. (actually, technically, that can be
                    optimized away too in many cases)
                    [color=blue]
                    >The same is not true for static variables in a function. So shouldn't the
                    >implementati on defer the memory allocation also
                    >for such variables till their first invocation just like construction?[/color]

                    The implementation has that freedom.
                    --
                    Greg Comeau/ 4.3.0.1: FULL CORE LANGUAGE, INCLUDING TC1
                    Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
                    World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
                    Comeau C/C++ with Dinkumware's Libraries... Have you tried it?

                    Comment

                    • cody

                      #11
                      Re: static allocation

                      > When do static members allocate memory? (at load time)[color=blue]
                      >
                      > How about static variables within a method?
                      >
                      > Where is the memory for static variables - is it allocated on the heap or
                      > does it have a region of its own?[/color]


                      statics are like global variables but reside in a class. they are already
                      contructed when the app starts and destroyed when the app ends so allocation
                      and deallocation of them is nothing you have to worry about. they reside in
                      their own memory location, where exactly this is is depends on the specific
                      implementation but that does not matter for programming.

                      --
                      cody

                      [Freeware, Games and Humor]
                      www.deutronium.de.vu || www.deutronium.tk


                      Comment

                      • Gianni Mariani

                        #12
                        Re: static allocation

                        cody wrote:[color=blue][color=green]
                        >>When do static members allocate memory? (at load time)
                        >>
                        >>How about static variables within a method?
                        >>
                        >>Where is the memory for static variables - is it allocated on the heap or
                        >>does it have a region of its own?[/color]
                        >
                        >
                        >
                        > statics are like global variables but reside in a class.[/color]

                        I think the OP asked for a static method variable.

                        static method variables are initialized the first time the statment
                        where is is defined is called and are destroyed upon program termination.

                        The code below shows how it all works.


                        #include <iostream>

                        struct foo
                        {
                        const char * str;

                        foo( const char * i_str )
                        : str( i_str )
                        {
                        std::cout << "making foo" << str << "\n";
                        }

                        ~foo()
                        {
                        std::cout << "deleting foo" << str << "\n";
                        }

                        void useme()
                        {
                        std::cout << "using foo" << str << "\n";
                        }
                        };


                        struct zoo
                        {
                        void method( int v = 0 )
                        {
                        static foo static_method_v ariable( "First" );
                        static_method_v ariable.useme() ;

                        if ( v == 0 )
                        {
                        static foo static_method_v ariable( "Second" );
                        static_method_v ariable.useme() ;
                        }
                        else if ( v == 1 )
                        {
                        static foo static_method_v ariable( "Third" );
                        static_method_v ariable.useme() ;
                        }
                        else if ( v == 2 )
                        {
                        static foo static_method_v ariable( "Fourth" );
                        static_method_v ariable.useme() ;
                        }

                        }
                        };


                        int main()
                        {
                        std::cout << "in main \n";

                        zoo zoo_obj1;

                        zoo zoo_obj2;

                        zoo_obj1.method ( 1 );

                        zoo_obj2.method ( 1 );

                        zoo_obj1.method ( 0 );

                        zoo_obj2.method ( 0 );

                        std::cout << "leaving main \n";
                        }

                        ---- produces ----

                        in main
                        making fooFirst
                        using fooFirst
                        making fooThird
                        using fooThird
                        using fooFirst
                        using fooThird
                        using fooFirst
                        making fooSecond
                        using fooSecond
                        using fooFirst
                        using fooSecond
                        leaving main
                        deleting fooSecond
                        deleting fooThird
                        deleting fooFirst



                        they are already[color=blue]
                        > contructed when the app starts[/color]

                        .... they are initialized when the code containing the statment is first
                        passed.

                        and destroyed when the app ends so allocation[color=blue]
                        > and deallocation of them is nothing you have to worry about. they reside in
                        > their own memory location, where exactly this is is depends on the specific
                        > implementation but that does not matter for programming.[/color]

                        Comment

                        Working...