I've been playing around with the Compiler Explorer recently. I loaded one of their examples that takes pointer parameters and changed it to instead take unique_ptr parameters. But I noticed that in the output assembly, calls to operator delete were conspicuously absent. I'm curious if anyone knows why.
Here's an example you can paste into the explorer. Be sure to also put -O3
in the compiler options.
#include <memory>
using std::unique_ptr;
void maxArray(unique_ptr<double[]> x, unique_ptr<double[]> y) {
for (int i = 0; i < 65536; i++) {
if (y[i] > x[i]) x[i] = y[i];
}
}
EDIT: Also for comparison, if instead I paste in one of the code examples from cppreference, then I do get operator delete in the output.
#include <iostream>
#include <memory>
struct Foo
{
Foo() { std::cout << "Foo::Foo\n"; }
~Foo() { std::cout << "Foo::~Foo\n"; }
void bar() { std::cout << "Foo::bar\n"; }
};
void f(const Foo &)
{
std::cout << "f(const Foo&)\n";
}
int main()
{
std::unique_ptr<Foo> p1(new Foo); // p1 owns Foo
if (p1) p1->bar();
{
std::unique_ptr<Foo> p2(std::move(p1)); // now p2 owns Foo
f(*p2);
p1 = std::move(p2); // ownership returns to p1
std::cout << "destroying p2...\n";
}
if (p1) p1->bar();
// Foo instance is destroyed when p1 goes out of scope
}
EDIT: +1 to krzaq. It's the caller, not the callee, that constructs and destroys parameters.