What's the memory layout of a class if it contains Containers like std :: map in the VC compiler
ClassA{
public:
std::map<int,int> m_map;
...
...
};
I've tried /d1reportSingleClassLayout
in visual studio but can't really decode the output:
1> 0 | ?$map@HHU?$less@H@std@@V?$allocator@U?$pair@$$CBHH@std@@@2@ m_map
1>28 | ......
My confusion is that containers are not fixed in size, so a pointer must exist in the Obj memory layout of class A to point to the actual contents of m_map on the heap?
source to share
The exact layout of your implementation std::map
depends on the people who wrote your standard library. It is not defined by C ++.
You can examine the standard headers on your build machine, otherwise you can just forget about it and code the standard APIs.
But, yes, there's a pointer or two in there somewhere, pointing to the dynamically allocated memory. Potentially quite a lot.
source to share
To answer your questions:
-
Will the pointer point to m_map on the heap? ... This is a question that only creators can answer
std::map
. I would say yes, but you'll have to look at the implementationmap
to find this eventually. In order for the map to develop dynamically, there must be a pointer. -
Will the default destructor of class A automatically free the space allocated for m_map? Yes, they were designed with this in mind, so the programmer didn't have to worry about memory. However, the method in which it does this is also in implementation
std::map
.
I think it's wise not to worry too much about it. The API was designed to ease the worry you mentioned.
source to share