So I was reading some Stack Overflow answers about deleting pointer arguments particularly these ones(1,2), because I am building a function, that requires a pointer as an argument.
A simplified version of the function is below:
void draw(Shape * b)
{
//Draws code....
}
No what I am confused about here is deletion. For example, if the function is called like this:
Shape * c;
draw(&c);
Then I don't have to delete anything. But if it is like this:
draw(new Shape{});
then I have to. So basically, my question is, how should I go about deleting if the new
keyword is used in the parameter. There are no possible exceptions that could be thrown in the functions, so no need for RAII. Any ideas? Please don't suggest anything involving smart pointers, because that is what I already will do, this question is curiosity. Also, please answer knowing that the function can take both the new operator, or an existing pointer, basically meaning I need a way to differentiate between both. Also, for my links: These don't really answer my question, because most of them just depend on smart pointers, or one call or the other.
You don't need to delete it, and, moreover, you shouldn't delete it. If earth is an automatic object, it will be freed automatically. So by manually deleting a pointer to it, you go into undefined behavior. Only delete what you allocate with new .
When we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value. So any change made by the function using the pointer is permanently made at the address of passed variable. This technique is known as call by reference in C.
delete keyword in C++ New operator is used for dynamic memory allocation which puts variables on heap memory. Which means Delete operator deallocates memory from heap. Pointer to object is not destroyed, value or memory block pointed by pointer is destroyed.
Yes to both. Pointers are passed by value as anything else. That means the contents of the pointer variable (the address of the object pointed to) is copied.
Now what I am confused about here is deletion.
This is precisely why we never pass raw pointers as arguments.
Here are some rules of thumb you may want to consider:
You may not alter the shape I am passing you:
void draw(const Shape& shape);
You may alter the shape, but I am retaining ownership of it:
void draw(Shape& shape);
Please use a copy of my shape:
void draw(Shape shape);
Please take ownership of this shape away from me:
void draw(std::unique_ptr<Shape> shape);
Let's share this shape:
void draw(std::shared_ptr<const Shape> shape);
You might use
void draw(std::observer_ptr<Shape> shape)
or
void draw(Shape& shape)
over
void draw(Shape * shape)
To be explicit that draw
doesn't reclaim ownership.
and use smart pointer in signature when you reclaim it.
void Take(std::unique_ptr<Shape> shape);
or
void Take(std::shared_ptr<Shape> shape);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With