-3

I am a new C++ learner. I have a simple question. Here is the code from a book. Is it necessary to create a new pointer, "pCat"? Can I just use the "rCat" and delete it? If not, why?

class SimpleCat
{
public:
    SimpleCat (int age, int weight);

    ~SimpleCat() {}

    int GetAge() { return itsAge; }
    int GetWeight() { return itsWeight; }

private:
    int itsAge;
    int itsWeight;
};

SimpleCat::SimpleCat(int age, int weight):
    itsAge(age), itsWeight(weight) {}

SimpleCat* TheFunction();

int main()
{
    SimpleCat* rCat = TheFunction();

    int age = rCat->GetAge();

    std::cout << "rCat is " << age << " years old!\n";

    std::cout << "rCat: " << rCat << "\n";
    // How do you get rid of that memory?
    SimpleCat* pCat = rCat;
    delete pCat;
    return 0;
}

SimpleCat* TheFunction()
{
    SimpleCat *pFrisky = new SimpleCat(5,9);``
    std::cout << "pFrisky: " << pFrisky << "\n";
    return pFrisky;
}
TriskalJM
  • 2,393
  • 1
  • 19
  • 20
Yue
  • 23
  • 6
  • Yes you can just do `delete rCat`. Why do you think you would not be able to do that? – Some programmer dude Jun 07 '17 at 13:14
  • 5
    If that's code from a book a would recommend to throw it away. –  Jun 07 '17 at 13:20
  • @manni66: Indeed, both the book and the code. – Bathsheba Jun 07 '17 at 13:20
  • My solution is only having the "rCat". It works. But the book gives the answer with 2 pointers. That problem talks about the memory leak. I am not familiar with it. I am wondering the one pointer solution may have trouble. – Yue Jun 07 '17 at 13:22
  • 1
    In this little program there is no reason for the second pointer `pCat`. It just doesn't make any sense to add another variable that will point to the exact same object just for deleting it. – Some programmer dude Jun 07 '17 at 13:23

2 Answers2

3

Given that definition of TheFunction, yes, you should eventually delete the return value. Note you could just delete rCat; directly - you don't need to copy it into another pointer pCat and then delete that.

However, it would be better if TheFunction returned a std::unique_ptr<SimpleCat> instead of a raw SimpleCat* pointer.

First, this makes it clear that the returned value is something that must be cleaned up, and not for example a pointer to something actually stored somewhere else.

Second, the caller wouldn't need to actually use delete at all, since the unique_ptr object does that for you.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • 3
    Good advice on using `std::unique_ptr`: have an upvote. – Bathsheba Jun 07 '17 at 13:17
  • Nice answer. Thank you. Now I learn a new command, std::unique_ptr – Yue Jun 07 '17 at 13:29
  • @Yue You should [pick up a new book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) if it is suggesting you do all this stuff without having given you enough background knowledge. `delete` is an operator and `std::unique_ptr` is a template class, nothing in c++ is called a command – Passer By Jun 07 '17 at 15:56
0

The rule is simple, you need to call delete on any pointer that's been allocated using new. (And you need to call delete exactly once.)

It's not necessary to assign the pointer to another variable, and call delete on that.

So therefore you could be even more minimal:

delete TheFunction();

would work too, although, of course, you wouldn't be able to do much with the cat.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483