June 2017
Intermediate to advanced
532 pages
12h 59m
English
Look at the following code. What if you are told that it contains a potential memory leak?
void function(shared_ptr<A>, shared_ptr<B>, int);// "function" is defined somewhere else// ...somewhere later in the code:function(new A{}, new B{}, other_function());
"Where is the memory leak?", one might ask, since the newly allocated objects A and B are immediately fed into shared_ptr types, and then we are safe from memory leaks.
Yes, it is true that we are safe from memory leaks as soon as the pointers are captured in the shared_ptr instances. The problem is a bit fiddly to grasp.
When we call a function, f(x(), y(), z()), the compiler needs to assemble code that calls x(), y(), and z() first so that it can forward their return ...
Read now
Unlock full access