allocation of memory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeyshree
    New Member
    • Jul 2010
    • 75

    allocation of memory

    hello,
    will someone please explain me when and where is memory allocated for static variables which are declared inside a function and static variable declared outside of all functions(globa l) and also for global variables which are not static?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    All those types of variables have data reserved and initialised for them in the programs data segment by the c start-up code which runs after the program has started and before main is entered.

    Comment

    • jeyshree
      New Member
      • Jul 2010
      • 75

      #3
      Originally posted by Banfa
      All those types of variables have data reserved and initialised for them in the programs data segment by the c start-up code which runs after the program has started and before main is entered.
      please explain me a little more i cannot not understand

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        A common problem is for different people to use different terms for the same concept; or worse, the same term for different concepts. Perhaps you should tell us a little of what you understand about memory allocation. Then we can be careful to phrase the responses more carefully.

        Also, it would help to understand why you are asking this question. You may have a larger issue that we could address directly.

        banfa: too bad I don't have any work done on your suggestion yet. ;-)

        Comment

        • jeyshree
          New Member
          • Jul 2010
          • 75

          #5
          Originally posted by donbock
          A common problem is for different people to use different terms for the same concept; or worse, the same term for different concepts. Perhaps you should tell us a little of what you understand about memory allocation. Then we can be careful to phrase the responses more carefully.

          Also, it would help to understand why you are asking this question. You may have a larger issue that we could address directly.

          banfa: too bad I don't have any work done on your suggestion yet. ;-)
          sir,
          please look at the following snippet
          Code:
                 #include<stdio.h>
                 #include<conio.h>
                static int a;   //static variable declared globally
                 main()
                 {
                 int fun();
                 clrscr();
                 fun();
                 getch();
                  }
                  int  fun()
                  {
                  static int b;//static variable declared locally
                  printf("hai");
                  }
          my question is when is memory allocated for variables a and b?they say that memory static variables get created in data segment which cannot be extented later in the run time of program.since static variable b is created inside function i assumed that variable b will be allocated memory only when it enters the function.
          but later knowing about the data segment and its inability to extend the size i came to the conclusion that i am wrong.is my conclusion a correct one?if so will static get created in stack or will some other mechanism take place?please explain.
          Last edited by Banfa; Jul 17 '10, 04:14 PM. Reason: Please enclose your code in [code] ... [/code] tags

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            All variables (static or not) defined outside a function are allocated before main() starts.

            All variables (static or not) defined inside a function are allocated when the function is called. The single variation is the static variable defined inside a function. In this case, the compiler generates code to allocate the variable when the function is first called. For all subsequent calls, the variable is not re-allocated but instead the variable allocated by the first call is used.

            These static variables and any global variables (static or not) are kept in an area of memory that remains allocated for the duration of the program.

            BTW: A static global variable is no different from a regular (extern) global variable. Here the "static" refers to the linkage. Static global variables can referred to only in the file where they are defined whereas the extern global variables can be referred to from any file in the program.

            Comment

            • jeyshree
              New Member
              • Jul 2010
              • 75

              #7
              Originally posted by weaknessforcats
              All variables (static or not) defined outside a function are allocated before main() starts.

              All variables (static or not) defined inside a function are allocated when the function is called. The single variation is the static variable defined inside a function. In this case, the compiler generates code to allocate the variable when the function is first called. For all subsequent calls, the variable is not re-allocated but instead the variable allocated by the first call is used.

              These static variables and any global variables (static or not) are kept in an area of memory that remains allocated for the duration of the program.

              BTW: A static global variable is no different from a regular (extern) global variable. Here the "static" refers to the linkage. Static global variables can referred to only in the file where they are defined whereas the extern global variables can be referred to from any file in the program.
              "A data segment is one of the sections of a program in an object file or in memory, which contains the global variables and static variables that are initialized by the programmer. It has a fixed size, since all of the data in this section is set by the programmer before the program is loaded. However, it is not read-only, since the values of the variables can be altered at runtime. "
              i got it from
              http;//enwikipedia.org/wiki/data_segment
              this is where my doubt starts.i agree that static variable gets created inmemory only once .but if it gets created only when function is called.by that time data segment cannot expand.so what happens?please explain this is my doubt

              Comment

              • jeyshree
                New Member
                • Jul 2010
                • 75

                #8
                Originally posted by donbock
                A common problem is for different people to use different terms for the same concept; or worse, the same term for different concepts. Perhaps you should tell us a little of what you understand about memory allocation. Then we can be careful to phrase the responses more carefully.

                Also, it would help to understand why you are asking this question. You may have a larger issue that we could address directly.

                banfa: too bad I don't have any work done on your suggestion yet. ;-)
                "A data segment is one of the sections of a program in an object file or in memory, which contains the global variables and static variables that are initialized by the programmer. It has a fixed size, since all of the data in this section is set by the programmer before the program is loaded. However, it is not read-only, since the values of the variables can be altered at runtime. "
                i got it from
                http;//enwikipedia.org/wiki/data_segment
                this is where my doubt starts.i agree that static variable gets created inmemory only once .but if it gets created only when function is called.by that time data segment cannot expand.so what happens?please explain this is my doubt

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  I agree with weaknessforcats that all variables defined outside a function are allocated [and initialized] before main() starts. I disagree with him that all static variables defined within a function are allocated (and initialized) when the function is first called. I suggest that it is permissible, but necessarily true, for the static variables defined within a function to also be allocated (and initialized) before main() is called. Notice that there is no perceptible difference between these two possibilities. "A difference that makes no difference is no difference."

                  Quoting the definition of "data segment" illustrates my concern with your recent "doubt" posts. You ask general questions about implementation-dependent issues. A response that is restricted to what the Standard says is incomplete -- it doesn't really answer your question. A complete response is inaccurate -- it is only true for some subset of compiler implementations .

                  The C Standard doesn't mention data segments. It is true that data segments are a tool used by most, if not all, compilers. However, each compiler is free to define the specific attributes of this tool in whatever way seems most useful to the developers of that compiler. The definition you cite is typically true as far as it goes, but in the absence of any Standard on data segments a compiler vendor is not going to get in trouble with anybody if their implementation is able to dynamically resize a data segment while the program is running.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by donbock
                    I disagree with him that all static variables defined within a function are allocated (and initialized) when the function is first called.

                    Consider a program with 5000 functions that have static variables but you call none of these functions. It's not reasonable that 5000 static variables will be created and initialized before main starts. Especially in C++ where you may need to call a constructor that has arguments for the amount of memory needed.

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Well I have to say I was all set to wade into this in support of Dons point of view, but I decided a little testing was in order first.

                      Initially I tried testing putting a large static array of int inside a function and seeing if it need to be called to be created by checking the memory allocated to the program in the system monitor. Unfortunately the results where indeterminate, that is the amount of memory allocated to the program never really changed for a reason I have not determined.

                      However I then realised that if I allow myself to use C++ then I can easily test using a simple class with output from the constructor (of course I am not sure how valid it is to output before main has been entered but it appeared to work.

                      So here is my test program

                      Code:
                      #include <string>
                      #include <iostream>
                      
                      class Tester
                      {
                      public:
                          Tester(const char *name)
                          : me(name)
                          {
                              std::cout << "Constructing: " << me << std::endl;
                          }
                      
                          ~Tester()
                          {
                              std::cout << "Destructing: " << me << std::endl;
                          }
                      
                      private:
                          std::string me;
                      };
                      
                      static Tester tg("global");
                      
                      
                      void function()
                      {
                          std::cout << "Enter function" << std::endl;
                          static Tester tf("function");
                          std::cout << "Exit function" << std::endl;
                      }
                      
                      int main()
                      {
                          std::cout << "Enter Main" << std::endl;
                      
                          function();
                          function();
                      
                          std::cout << "Exit Main" << std::endl;
                      
                          return 0;
                      }
                      Nothng exceptional and here is the output

                      Code:
                      Constructing: global
                      Enter Main
                      Enter function
                      Constructing: function
                      Exit function
                      Enter function
                      Exit function
                      Exit Main
                      Destructing: function
                      Destructing: global
                      Clearly showing that the static variable inside function is constructed the first time it its line of code is executed in function.

                      So although my gut feeling was that weaknessforcats was wrong and that function scope static data is constructed at c start-up it turns out that I, and my gut, are wrong and weaknessforcats is right (something they have an irritating habit of being).

                      While I feel that there is room for checking what it says in the standard concerning this it is clear that for my platform(32bit Ubuntu/gcc) at the very least posts #6 and #10 accurately describe what is happening.

                      Comment

                      • Banfa
                        Recognized Expert Expert
                        • Feb 2006
                        • 9067

                        #12
                        Actually what I just said in post #11 has an implication for program efficiency. Because a function with static data in it has to branch round the code creating the data every time the function is called except the first time.

                        Where as the same function but with the data declared statically outside the function does not need to do this branch. It is conceivable for a function either with a large number of static variables or that is called extremely frequently that this hidden branch could cause undesirable inefficiencies.

                        Comment

                        • donbock
                          Recognized Expert Top Contributor
                          • Mar 2008
                          • 2427

                          #13
                          I don't use C++, so I'm not competent to say anything about the details of how a C++ compiler allocates memory for variables.

                          I'm a C programmer. Most of my experience is with cross-compilers for embedded microprocessors that lack operating systems. I'm quite positive (although certainty is not the proof of certitude) that these compilers simply allocate space in text or bss for function-scope static variables. That doesn't mean that other compilers don't work the way weaknessforcats described.

                          This just emphasizes my earlier point that questions about implementation-dependent features either have lots of correct answers or have no correct answer at all.

                          This scheme described by weaknessforcats and banfa reminds me of optimistic malloc in Linux, where memory isn't allocated until you try to use it. Once an optimistic memory manager is implemented, it isn't much of a stretch to extend it to handling static variables too. In that case you don't need conditional logic in the function, memory allocation is handled by the MMU exception handler. Banfa mentioned Ubuntu ... I wonder if weaknessforcats might have been describing a linux compiler too.

                          Comment

                          • weaknessforcats
                            Recognized Expert Expert
                            • Mar 2007
                            • 9214

                            #14
                            I wonder if weaknessforcats might have been describing a linux compiler too.
                            I have never heard of optimistic malloc. Basically, I am a C++ guy. In C++ memory is allocated using malloc(). I know about the new operator but actualy the new operator just calls malloc to get the memory and then calls the class default constructor, if necessary.

                            The thing with static in C++ is that you may need to call a funciton to create your static variable and it may be that the variable cannot be constructed until a file is read. Like this:

                            Code:
                            int* getValue()
                            {
                                 //assume code to read a parameter file
                                int * temp = new int[10]; //assume we read a 10 from  the parameter file
                            	return temp;   
                            }
                            
                            void function()
                            {
                                 static const int* var = getValue();
                            }
                            
                            int main()
                            {
                                function();
                            }
                            Here the function() has a const static array that it cannot initialize until getValue() is called to get the size of the array. Therefore, var cannot be allocated before the first call to function() because it is a const value and const values must be initialized when they are created. Further, getValue() can't be called until after main() has started.

                            The call to getValue() will occur only on the first call to function().

                            Comment

                            • donbock
                              Recognized Expert Top Contributor
                              • Mar 2008
                              • 2427

                              #15
                              "Optimistic malloc" refers to the memory allocation model in Linux. On linux, malloc never fails! It always returns a non-null pointer. However, the returned pointer points to nonexistent virtual memory. An MMU exception occurs when you dereference the pointer, the exception handler then truly allocates the necessary memory, and your program proceeds none the wiser. However, if there is no memory available, the notorious OOM Killer kills a task and recovers its memory. Typically, but not always, the task killed is the one whose deferred allocation failed.

                              This is a very brief overview of the linux memory allocation model. I left out lots of details and corner cases.

                              I'm not aware of any other platform that uses optimistic memory allocation.

                              My point was that perhaps the native linux compiler uses this same mechanism for static variables; and that might be why you see static variables popping into existence the first time their enclosing function is called. The only change needed to support this is for the MMU exception handler to also initialize the variable after truly allocating it. If so, then this would be linux-only behavior.

                              Comment

                              Working...