Memory leak in Return

Asked

Viewed 65 times

-1

I’m reading a book and I came across this code:

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();
    cout << "rCat is " << age << " years old!\n";
    cout << "&rCat: " << &rCat << endl;
    // How do you get rid of that memory?
    SimpleCat * pCat = &rCat;
    delete pCat;
    // Uh oh, rCat now refers to ??
    return 0;
}

SimpleCat &TheFunction()
{
    SimpleCat * pFrisky = new SimpleCat(5,9);
    cout << "pFrisky: " << pFrisky << endl;
    return *pFrisky;
}

My question is:

The book says there’s a memory leak in retun of the call for SimpleCat & rCat = TheFunction() due to memory allocation in scope. I wondered how to solve this.

1 answer

3


The function TheFunction does the following:

SimpleCat * pFrisky = new SimpleCat(5,9);

This line dynamically allocates an instance of the class SimpleCat, points your memory address to the variable (pointer) pFrisky and returns that memory address as a reference.

Already out on the call of main, the function TheFunction was called so:

SimpleCat & rCat = TheFunction();

So that dynamically created instance is now being referenced at another address, defined in a non-dynamic way in the variable rCat (who, note, is not a pointer).

Both of the calls below print the memory address where the object is located:

  1. cout << "pFrisky: " << pFrisky << endl (in function TheFunction)
  2. cout << "&rCat: " << &rCat << endl; (in function main)

I didn’t run to be sure, but you’ll probably realize that they are different addresses (because the second variable, rCat, was not dynamically allocated.

Edit: I ran it in Ideone to have certainty, and not necessarily the printed addresses shall be different. Still, objects are allocated in different places (read about the difference between the heap and stack right here), and for that the leak.

So when you try to delete that dynamically allocated area by doing:

SimpleCat * pCat = &rCat;
delete pCat;

In fact you’re making a beautiful of a cag... I mean, nonsense. :)

How to solve? If you allocate dynamically, return an actual pointer:

SimpleCat *TheFunction()
{
    SimpleCat * pFrisky = new SimpleCat(5,9);
    cout << "pFrisky: " << pFrisky << endl;
    return pFrisky;
}

There, in the main, you do:

SimpleCat * rCat = TheFunction();
. . .
delete rCat;
  • 1

    Only complementing that this is true for a simple exercise like this. Even if you don’t give the delete nothing bad will happen because the code will close soon after. In real and modern codes whenever you use a delete he’s probably doing something wrong. I’d already talked to him about it: http://answall.com/a/174932/101

  • "In real and modern code whenever you use a delete you’re probably doing something wrong." Fantastic! :)

  • obg cara I also made a change to this function the same way you showed me , but I was thinking that when the return of this function Thefunction();was assigned to Simplecat * rCat ,it would be pointing to a new Simplecat object in the case rCat

  • No, because in that case rCat is a pointer (was declared with *). And a pointer is nothing more than a whole that holds a memory address. So you simply return the address, instead of "copying" the content from one place to another.

  • nossa cara salvou minha pele valeu mesmo , so uma ultima pergunta tentei imprimir os enderenços dps dessa alteração na funçao, mas pq o enderenços do ponteiro simpleCat * rCat nao apontava para o do ponteiro da funçao SimpleCat * pFrisky = new SimpleCat(5,9);

  • pq wanted to see thought that the Simplecat address * pFrisky = new Simplecat(5,9); would be the same as Simplecat * rCat

  • in the case Simplecat * rCat would have to have the Simplecat address * pFrisky = new Simplecat(5,9); but always gave different so thought q returns a pointer in the function was not working

  • Not at all. If the answer helped you, please consider accept it. It should be the same printed pointer. Make sure you haven’t made any other mistakes. Ah, and if you have another related question: open a new question, ok?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.