Sign in to follow this  
RandomAxess

C++ Free Store and Stack question

Recommended Posts

Lets say i've got the following struct: struct Tri { float v[3]; }; then i've got this class mesh: class Mesh { public: Mesh(); ~Mesh(); private: vector<Tri> faces; }; now, in my main code, when i create a Mesh, i'll wanna create it as such: Mesh* m1 = new Mesh; when I do this, I am initializing the Mesh on the free-store (or heap, if i have the name backwards...). However, does the vector list member data get initialized on the free store as well? the new should allocate enough space on the free store to manage a vector datatype, but i wasn't sure where it would go. Would it be more memory-efficient to make it a vector<Tri>* faces? Or does the fact that I am initializing it on the free store take care of that?

Share this post


Link to post
Share on other sites
Quote:
Original post by RandomAxess
now, in my main code, when i create a Mesh, i'll wanna create it as such:

Mesh* m1 = new Mesh;

when I do this, I am initializing the Mesh on the free-store (or heap, if i have the name backwards...). However, does the vector list member data get initialized on the free store as well? the new should allocate enough space on the free store to manage a vector datatype, but i wasn't sure where it would go.
That line allocates a chunk of memory big enough to contain a Mesh on the heap/free store, and also allocates a pointer on the stack (4 bytes on a 32-bit system), which points to that larger chunk of memory.

The "faces" member of the Mesh class (Which is a vector) is contained in the memory that was allocated for the Mesh, which is put onto the heap.

Quote:
Original post by RandomAxess
Would it be more memory-efficient to make it a vector<Tri>* faces? Or does the fact that I am initializing it on the free store take care of that?
Nope, it wouldn't be more efficient. Because the parent object is on the heap, that object is too. It'd actually be slightly less efficient, since you need to store the memory for the vector, plus an extra 4 bytes (again, on a 32-bit system) for the pointer to the vector.

It may be worth pointing out that a vector internally uses a pointer to point at the allocated memory, so sizeof(std::vector<Tri>); is probably only going to be about 12 bytes (Pointer, size, capacity variables). So you'll have:
* Mesh (on heap) containing:
* The vector (on heap because parent object is on heap) containing:
* A pointer to more memory allocated on the heap.
That means that if you have a vector on the stack, you can put as much stuff in it as you like without running out of stack (Although you'll run out of heap soon eventually).

Share this post


Link to post
Share on other sites
Except for a very small footprint (usually a dozen bytes), std::vector allocates its memory using the provided allocator. The default allocator is std::alloc, which allocates memory using new. So, the means by which you allocate the vector itself are completely independent of how the memory for its contents will be allocated.

Share this post


Link to post
Share on other sites
With the new operator the mesh will be allocated on the heap. The internal memory in the vector will also be allocated on the heap in a different location. The vectors internal memory would even be allocated on the heap if your mesh object was allocated on the stack. Making it a vector<Tri> *faces would not make it more efficient, probably more inefficient as you would have to allocate the vector<Tri> with the new operator and free it yourself.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this