Hi
I am looking for someone kind enough to send me a refresher on memory allocation in c++ in the following case. The following code seems to work. However "std::set<int> a_set" is allocated on the stack and I would expect it to be freed when we exit init and it runs out of scope?
Thank you for your help
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
struct MyStruct {
void init() {
auto it = my_map_.find(2);
if(it == my_map_.end()) {
std::set<int> a_set;
a_set.insert(3);
auto ret = my_map_.insert(make_pair(2, a_set));
} else {
it->second.insert(3);
}
}
std::map<int, std::set<int> > my_map_
}
All containers uses copy constuctor of their elements to perform a copy of them, so if copy constructor is properly implemented (as it is in all standard library and primitive types) it would be a deep copy.
Structs (and classes) has a default copy constructor which does a good job unless you have pointer/non-copyable member inside.
Also you can use my_map_.emplace(2, a_set) to construct elements in place and avoid posiible unnesesary copies.
One more question. When I do my_map_.insert(make_pair(2, a_set));
Is it make_pair or insert that does call copy constructor for a_set?
You also anticipated my next question. The question was the following:
a_set gets copied via its copy constructor when inserted into m_map_.
Each element of the a_set gets also copied via their copy constructor.
Isn't inefficient? In that case are we better of allocating a_set into the heap and manipulate its pointer like something along the following code?
One more question. When I do my_map_.insert(make_pair(2, a_set));
Is it make_pair or insert that does call copy constructor for a_set?
make_pair calls copy constructor of a_set. insertwould call copy constructor of resulting pair which calls copy constructor of set if it wasn't temporary. In this case move constructors are called.
Yes, you can do emplace like you said, but you should avoid temporary copies (in your example temporary set is created and then moved into place)
correct way: my_map_.emplace(3, {1, 2});. This will create in place pair of integer 3 and set of two numbers: 1 and 2.