tags:

views:

156

answers:

6
// Assume class definition for Cat is here.

Cat makeCat() {
    Cat lady = new Cat("fluffy");
    return lady;
}

int main (...) {
    Cat molly = makeCat();
    molly->eatFood(); 
    return 0;
}

Will there be a "use after free" error on molly->eatFood()?

+2  A: 

There is no error as far as invalid use of memory other than a memory leak at the end of your program. If something is created on the heap (such as with new) then you need to call delete on it to free it.

You also have a lot of syntax errors, corrected below.

Cat* makeCat() 
{
  Cat *lady = new Cat("fluffy");
  return lady;
}

int main (int argc, char** argv) 
{

  Cat* molly = makeCat();
  molly->eatFood(); 

  delete molly;//This was added
  return 0;
}
Brian R. Bondy
Can one leak memory when the program terminates?
Eiko
@Eiko: Usually the OS will free it but this is not guaranteed. For anything that is not guaranteed it's highly unrecommended to do it. Especially with not freeing memory. For example who says an important piece of code doesn't exist in the destructor.
Brian R. Bondy
@Brian: I agree that it's wise to clean up after yourself - and with clean code it's not much work anyway. (Although I sometimes read the advice to not waste those CPU cycles)
Eiko
@Eiko: " I sometimes read the advice to not waste those CPU cycles" - Ignore that advice it is bad advice written by someone unexperienced.
Brian R. Bondy
@Brian R. Bondy: Be careful. In some **very** rare conditions it worth not doing a cleanup, but killing yourself. If you delete all allocated memory while you were swapped out by OS, then the closing of the program will eat up a load of resources and user time. The software must be able (compiling option) to exit cleanly, but sometimes it is really good to commit suicide. :-) For example I would welcome it in Firefox and Outlook and some...
Notinlist
A: 

I think it should be Cat *lady and Cat *molly, but else it should be ok.

Eiko
When I return the object **sally**, isn't that pass-by-value? If yes, won't **sally** get deleted after the function ends?
Kevin
If by 'sally' you mean the 'lady' variable inside of makeCat, no.
MerickOWA
You actually pass a pointer (but you missed the "*" in your code.
Eiko
+1  A: 

Since lady is created on the heap (with a new), it will not be destroyed when you exit the makeCat method. So the call on molly is perfectly valid.

BUT, you have a memory leak. You need to delete molly after using it (sometime in the future.) Since your program ends, this is not a big deal. In a larger program, this would be a very big deal.

Starkey
+2  A: 

new Cat("fluffy") creates a pointer. You will need to specify Cat* as return type. Since the object is created in heap, it will still be available after the function returned.

elusive
It is still available when created on the stack as it is returned by value (ie copy constructed back (though the compiler is allowed to elide the copy)). So apart from the syntax error the code is fine as written and does not use a destroyed object.
Martin York
+1  A: 

The problem is not a "Use after Free"; more probably that you're not delete-ing the new instance.

pascal
+2  A: 

Corrected your program and created an example implementation of class Cat:

#include <iostream>
#include <string>

class Cat {
public:
        Cat(const std::string& name_ = "Kitty")
        : name(name_)
        {
                std::cout << "Cat " << name << " created." << std::endl;
        }
        ~Cat(){
                std::cout << "Cat " << name << " destroyed." << std::endl;
        }
        void eatFood(){
                std::cout << "Food eaten by cat named " << name << "." << std::endl;
        }
private:
        std::string name;
};

Cat* makeCat1() {
        return new Cat("Cat1");
}

Cat makeCat2() {
        return Cat("Cat2");
}

int main (){
        Cat kit = makeCat2();
        kit.eatFood();

        Cat *molly = makeCat1();
        molly->eatFood();
        delete molly;

        return 0;
}

It will produce the output:

Cat Cat2 created.
Food eaten by cat named Cat2.
Cat Cat1 created.
Food eaten by cat named Cat1.
Cat Cat1 destroyed.
Cat Cat2 destroyed.

I suggest you learn a basic book about the C++ cover to cover before continuing.

Notinlist
thanks, Notinlist.
Kevin
...although it may produce a different output since the C++ standard doesn't require a compiler to elide unnecessery copies whenever possible. So, it's entirely possible to see another two "Cat Cat2 destroyed" messages (no RVO, for example)
sellibitze