Sign in to follow this  

when a class is in the Heap, where are its members?

Recommended Posts


a question that torments me for so much time.

Suppose I'm at the main() function and NEWed a class on the heap.

X x = new X;

and this X class holds inside itself many huge data structures like maps, sets, vectors, etc. But they are all declared like this

class X {
std::map< z, w> map;
std::vector< z> vec;


let's supposed they are filled and HUGE. Where are they located? in the Heap of the in the Stack?

Share this post

Link to post
Share on other sites
The map and vec objects will be located on the heap at a small offset from X. The actual data inside the map and vector objects is allocated from the heap as well, but could be anywhere.

This is how your X class might be laid out in memory:

X + 0: map
X + sizeof(map<z,w>): vec

If you do sizeof(map<z,w>) in your debugger you will see how big that object is. That will never change regardless of how much data you store in it.

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