The question you read is different. Say you had this code:
int cubed(int a) {
int* ptr = new int;
*ptr = a * a * a;
return *ptr;
}
Now, you'd be leaking the dynamically-allocated int
, because you'd return a copy of it and never delete
the original.
Your code is actually worse because, instead of allocating with new
, you're simply writing through an uninitialised pointer to memory that is not yours. In effect, you're writing to an int
that does not exist.
What you should be doing is this:
constexpr int cubed(const int a)
{
return a*a*a;
}
Or just invoke std::pow(value, 3)
at the call site.