Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I delete pointer passed to a function as argument?

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.

like image 214
Arnav Borborah Avatar asked Aug 24 '16 13:08

Arnav Borborah


People also ask

Do I need to delete a pointer?

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 you pass a pointer as an argument to a function?

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.

Does delete delete a pointer Why or why not?

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.

Are pointers copied when passed as an argument?

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.


2 Answers

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:

  1. You may not alter the shape I am passing you:

    void draw(const Shape& shape);

  2. You may alter the shape, but I am retaining ownership of it:

    void draw(Shape& shape);

  3. Please use a copy of my shape:

    void draw(Shape shape);

  4. Please take ownership of this shape away from me:

    void draw(std::unique_ptr<Shape> shape);

  5. Let's share this shape:

    void draw(std::shared_ptr<const Shape> shape);

like image 157
Richard Hodges Avatar answered Nov 03 '22 23:11

Richard Hodges


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);
like image 30
Jarod42 Avatar answered Nov 03 '22 22:11

Jarod42