Memory allocation during object formation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • awanish2820
    New Member
    • Apr 2010
    • 1

    #1

    Memory allocation during object formation

    Code:
    #include<iostream.h>
    class harsh
    {
     private:
    	int acc;
    	int pass;
     public :
                       harsh(int a,int p)  //step2
    	{
                          acc=a;               
    	   pass=p;              
                        }
    
    
    };
    void main()
    {
     harsh h1;                               //step1 
    }
    /*Question.. When the memory is allocated for object h1?
    Answer..
    1)at step1
    2)at step2
    */
    *Explain in details using pointer and memory block..
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Its hard to say since you return void from main you have invoked undefined behaviour because main always returns int like so
    Code:
    int main()
    {
        return 0;
    }
    Given that undefined behaviour is invoked the compiler could do anything, it might allocate 0 bytes of memory it might allocate 1Mbyte, it might cause demons to fly out of your nose and it might just act as if undefined behaviour has not been invoked.

    What do you think this code does?
    What does harsh contain?
    What is the size of each of these things?
    How big do you think that makes harsh?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Read up on constructors.

      Constructors do not create objects just as destructors do not destroy objects.

      Memory is allocated when objects are defined, this is when they are created.

      Comment

      • hype261
        New Member
        • Apr 2010
        • 207

        #4
        Memory Allocation

        Because harsh is on the stack the memory is allocated for the harsh object here

        void main
        { <--------

        the object isn't initialized until it gets to

        harsh h1;

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Code:
          Because harsh is on the stack the memory is allocated for the harsh object here
          
          void main
          { <--------
          C does this so all your stack variables must appear at the beginning of the function before any instructions.

          This cannot be allowed in C++ becuse it permits uninitialized variables to be processed.


          Therefore, C++ uses dynamic stack allocation since the size and number of the local variables is not known at compile time. Therefore, the memory is allocated at the place the object is defined followed immediately by a constructor call to initialize the object. That is to say the size of the C++ stack frame is no known at compile time.

          Comment

          • hype261
            New Member
            • Apr 2010
            • 207

            #6
            Maybe I am wrong

            Maybe I am incorrect in my understanding, but if a function call stack frame consists of Function arguments , Return address, and Local Variables then how can memory not be allocated when the function enters scope?

            In Bruce Eckels book he says:

            When a C++ object is created two events occur:

            Storage is allocated for the object
            The constructor is called to initialize the storage.

            Storage can be created on the stack whenever a particular execution point is reached (an opening brace). That storage is released automatically at the complementary execution point (the closing brace).

            I could be reading this incorrectly...

            Comment

            • Banfa
              Recognized Expert Expert
              • Feb 2006
              • 9067

              #7
              Originally posted by weaknessforcats
              C does this so all your stack variables must appear at the beginning of the function before any instructions.
              Not strictly true variables in C can appear at the top of any block enclosed by braces, that might be a function or an if or for statement or a set of braces put in place specifically to declare another variable (a debug technique rather than good programming practice).

              Originally posted by weaknessforcats
              Therefore, C++ uses dynamic stack allocation since the size and number of the local variables is not known at compile time.
              This also seems wrong. The compiler does know the size and number of local variables required at a specific point in the code at compile time, what it does not know is the size and number of all the local variables that may be declared in a function at the time the opening brace of the function is parsed.

              However by my first statement that is also true of C.

              If a given compiler C or C++ output the code after parsing the entire function then I see no reason why it shouldn't allocate all the memory required when the function enters. If the data is for an object that has not actually been declared in the code yet it would have to be considered memory reserved for the object but not actually allocated to the object yet. In place construction would facilitate this.

              Not only that but there is nothing in the standard that forbids this since the standard does not even mandate a stack although that isn't to say there are not practical obsticles to this scheme, I particularly have not given any thought to how stack unwinding and object deletion would be handled in the case of an exception.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by hype261
                Storage can be created on the stack whenever a particular execution point is reached (an opening brace). That storage is released automatically at the complementary execution point (the closing brace).
                I see where our confusion is. In the example, the object was a local stack variable that was in scope. Therefore the memory was allocated when the scope was entered. That is, at the beginning of main(). However, functions can have many levels of braces and at each of those levels stack memory can be allocated. Therefore, all stack memory cannot be allocated when the function is called.

                Apparently, I misread your comment which is true for the example

                Comment

                Working...