-
January 20th, 2011, 10:09 AM
#1
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;
}
-
January 20th, 2011, 10:14 AM
#2
Re: Objects of a class
Your choices are heap or stack. Since it's not heap...
-
January 20th, 2011, 10:18 AM
#3
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.
-
January 20th, 2011, 10:44 AM
#4
Re: Objects of a class
Originally Posted by Lindley
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.
-
January 20th, 2011, 10:55 AM
#5
Re: Objects of a class
Originally Posted by Learned
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.
-
January 20th, 2011, 12:03 PM
#6
Re: Objects of a class
Originally Posted by GCDEF
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).
-
January 20th, 2011, 01:06 PM
#7
Re: Objects of a class
Originally Posted by Learned
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|