Objects of a class
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Objects of a class

  1. #1
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    Objects of a class

    Where are the objects of a class created? It is definitely not on heap if the declaration is something like:

    Code:
    class A
    {
    
    };
    
    int main()
    {
         A a1;
    }

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: Objects of a class

    Your choices are heap or stack. Since it's not heap...

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Objects of a class

    Keep in mind that regardless of where the object itself is created, the object or one of its members may internally use heap memory.

  4. #4
    Join Date
    Oct 2009
    Location
    NY, USA
    Posts
    191

    Re: Objects of a class

    Quote Originally Posted by Lindley View Post
    Keep in mind that regardless of where the object itself is created, the object or one of its members may internally use heap memory.
    Can you please explain this?

    Also if the object is created on stack why do we need destructor? In the main(), objects would behave as automatic variable and get destroyed automatically right before the scope of main ends. In the same manner as for any other function.

    E.g.
    Code:
    void f()
    {
    double d;
    }
    The object d gets destroyed automatically when the scope of function is over.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,067

    Re: Objects of a class

    Quote Originally Posted by Learned View Post
    Can you please explain this?

    Also if the object is created on stack why do we need destructor? In the main(), objects would behave as automatic variable and get destroyed automatically right before the scope of main ends. In the same manner as for any other function.

    E.g.
    Code:
    void f()
    {
    double d;
    }
    The object d gets destroyed automatically when the scope of function is over.
    The destructor is for the object to clean up any memory it may have used. In your simple example, you don't need one, but if the object contained a pointer to something it had allocated on the heap, it would need to delete that memory, and the destructor is the best place to do that.

    The object getting deleted when it goes out of scope has nothing to do with whether you need a destructor or not. As Lindley said, the object itself may reside on the stack, but that doesn't mean it isn't allocating stuff on the heap.

  6. #6
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    3,827

    Re: Objects of a class

    Quote Originally Posted by GCDEF View Post
    Your choices are heap or stack. Since it's not heap...
    You're forgetting global data. Which is neither on the heap nor the stack.
    It's in the .data or .rdata segment. (note that it can be in rdata safely if it has only const data, but does have a constructor/destructor).

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Objects of a class

    Quote Originally Posted by Learned View Post
    Can you please explain this?
    Consider a std::vector<int>. The vector object itself is always the same size (which you can check with sizeof()), but it can hold variable numbers of elements in its array. Clearly, then, the array is not directly inside the object. Instead, the vector holds an internal pointer to heap memory where the array is managed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center