Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

When to use deep copying and Move constructor(move semantics C++)?

+2 votes
asked Dec 19, 2021 by Areeb Sherjil (2,000 points)

I have a constructor that takes into two pointers to objects and then assigns the memory to the pointer to objects that it contains as private member variables.

this is the class definition in the .h file:

class PowerComputation
{
public:
    PowerComputation(Numerator* num, Denom* denom,int maxPower);// take in pointers
    friend std::ostream& operator<<(std::ostream& output, const PowerComputation&);
private:
    Numerator* num;
    Denom* denom;
    int maxPower{};// future functionality will be added for maxPower computation
};


//----------------------------------------------------this is the corresponding implementation of the constructor in the

//-----------------------------------------------------.cpp file see below


PowerComputation::PowerComputation(Numerator* num, Denom* denom, int maxPower)
{
    this->num = num;
    this->denom = denom;
    this->maxPower = maxPower;
}


//-------------------------------------------------------implementation in the main.cpp file

Numerator* ptr1 = &(num1 / den1); // perform the calculation(the divide operator is overloaded)

Denom* ptr2 = &den1;

PowerComputation displayPower(ptr1, ptr2, temp2);


Is this okay?  The code is functional but is there a need to use deep copy or Move constuctors. The pointers is to variable in the stack not heap so no need to delete them.

Another question:

Would it be better to make the pointer to Denom & Numerator objects shared_ptr since there are two pointers pointing to the same memory address?

1 Answer

0 votes
answered Dec 19, 2021 by Peter Minarik (63,320 points)
selected Dec 22, 2021 by Areeb Sherjil
 
Best answer
Numerator* ptr1 = &(num1 / den1); // perform the calculation(the divide operator is overloaded)

Denom* ptr2 = &den1;

Is this okay?

It depends...

  1. What will you do with ptr2? As long as ptr2 or any copy of it (e.g. passing to another function) does not outlive the scope of den1 you're good. If for instance, you return ptr2, and start dereferencing it outside of the scope of den1 you will have a funny behaviour as will be den1 out of scope and potentially destroyed, so you shouldn't reference its memory address anymore.
  2. ptr1 looks fishy to me. I'm not sure what the result of (num1 / den1) is, but if it's not an lvalue, you won't be able to take its memory address. A temporary object's address shouldn't be taken.

So it's up to you how you use it and whether you do something with it that you're not supposed to.

The pointers is to variable in the stack not heap so no need to delete them.

If your code receives a pointer, then your code shouldn't be responsible for destroying it, so it does not matter whether a pointer (an argument of your function) points to stack or heap, let the person/code who created it be responsible for releasing it.

Would it be better to make the pointer to Denom & Numerator objects shared_ptr since there are two pointers pointing to the same memory address?

Absolutely. Modern C++ should make use of std::shared_ptr to facilitate better pointer handling, help avoiding double-free or resource leakage.

commented Dec 20, 2021 by Areeb Sherjil (2,000 points)
edited Dec 20, 2021 by Areeb Sherjil
This is the full code, I don't understand what you are saying about the pointers.
https://onlinegdb.com/0g9dAELVi
(I updated the code to add shared pointers.)20/12/2021

The overloaded division operator returns 'this' pointer after modifying the num1 object. I will research on how to implement smart pointers instead. Can you have a look at my code and see it pointers go out of scope?
commented Dec 22, 2021 by Peter Minarik (63,320 points)
Hi. I checked your code.

I think it is fine as you create the shared pointers in the main() function:

std::shared_ptr<Numerator>ptr1 = std::make_shared<Numerator>((num1 / den1)); // perform the calculation
std::shared_ptr<Denom>ptr2 = std::make_shared<Denom>(den1);
PowerComputation displayPower(ptr1, ptr2, temp2);

Since both num1, den1, ptr1 and ptr2 have the same scope (the main() function) there is no problem like the pointer (e.g. ptr1) outliving the lifespan of the pointed object (num1).
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...