dervel
dervel

Reputation: 78

Destructors in C++ (Compared to java)

So far I have been writing programs in Java. So when I started C++, the first thing that came to my mind was how to destruct/delete/finalize objects I don't need anymore.

With Java I used to set them to null so the garbage collector was taking care of it. However, I don't know how things worth with C++. I found this article http://en.wikipedia.org/wiki/Comparison_of_Java_and_C%2B%2B which solved most of my questions. But there are still a few things I didn't understand.

1) In Java there is a way to force the garbage collector to clean right on the spot(which is not always useful, since it waits for a few trash to stack up before running). Is there a way to do that with C++?

2) (C++) Also the opposite of the above, how can i make it so that i put the object on a state of "marked to be deleted" and the programm decides when to clean it (like Java)?

3) (C++)Should i force the barbage collector to clean right on the spot(i am pretty sure that's not the right way but i am asking just to be sure)?

I would appriciate it if you could give a small code example with which code triggers what.

Upvotes: 3

Views: 811

Answers (5)

Nathan Monteleone
Nathan Monteleone

Reputation: 5470

Garbage collection in C++ is always immediate. There's no separate garbage collector; when you delete an object it's deleted immediately on the current thread. It looks like this:

MyObject* foo = new MyObject();
...
delete foo;

There are garbage collection frameworks available for C++, and you can also look into smart pointers which are also a form of garbage collection.

Note James' comments below -- the destructor and operator delete for the object are always called immediately, but it's implementation-dependent as to whether or not the memory will be available right away.

Upvotes: 1

Mooing Duck
Mooing Duck

Reputation: 66961

C++ is very different than Java in this area, so here's a brief overview:

allocation: memory is set aside for an object.
construction: The object is prepared to be used.
destruction: The object "finishes" everything and disassembles itself.
deallocation: the memory is given back to the system.

int main() {
    int myint;  //automatic int object is allocated and constructed
    //stuff
}   // when main ends, automatic int object is destroyed and deallocated

int main() {
    int* mypointer;  //automatic pointer object is allocated and constructed
    mypointer = new int;  //dynamic int object is allocated and constructed
    //stuff
    delete mypointer; //dynamic int object is destroyed and deallocated 
}   // when main ends, automatic pointer object is destroyed and deallocated
    // note: Pointers to _not_ delete the object they point to.

class myclass {
    //members
public:
    myclass() {} //this is the default constructor
    myclass(const myclass& rhs) {} //this is the copy constructor
    myclass& operator=(const myclass& rhs) {return *this} //this is the assignment operator
    ~myclass() {} //this is the destructor
};

When a function ends, all the variables in the function itself (which we call automatic) have their destructors called, and then they are deallocated automatically. This means for objects local to a function, they automatically clean themselves the instant the function ends. This also applies magically to members of a class. When it is destroyed, each of it's members will automatically be destroyed. This means most destructors are empty.

If you allocate stuff manually (with the new keyword), it must be destroyed and deallocated manually with the delete keyword. When you call delete, it will destroy (and deallocate) right there and then, and won't continue until it is done. If you forget, it WILL NOT EVER GET DEALLOCATED (altough, some operating systems will deallocate it when your program ends).

Since people make mistakes, the "correct" thing to do when you need dynamic objects is:

int main() {
    std::unique_ptr<myclass> myptr = new myclass(); //allocate and construct
} //both the unique_ptr and the dynamic object are destroyed and deallocated

and the unique_ptr is smart enough to automatically clean up the thing it points at, freeing you for bigger concerns.

The reason C++ does this is because if you have a object F that represents that file, it might have a exclusive lock on that file. In C++, once F is destroyed, you can immediately create an object G that uses that same file. In Java, there's no guarantee that the finalizer will ever run, meaning that file may remain locked until your program ends. (Unlikely, but possible)

Upvotes: 4

vitakot
vitakot

Reputation: 3844

C++ uses RAII (Resource Acquisition Is Initialization) programming idiom, there is nothing like automatic memory management known as Garbage Collector in java or AutoZone in Objective-C 2. Thus the proper instance clean-up can easily get complicated. To answer your questions:

ad 1: There is no GC in C++ so you have to delete your objects manually or use Reference Counting technique or better Smart Pointers, which are now part of the C++11 standard, but as far as I know it is not available in any C++ compiler yet. For now you can use Smart Pointer templates from Boost library: http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/smart_ptr.htm. The new C++ standard took directly the Boost implementation so there will be no problem when switching to the new standard in the near future (MSVC 2012 will implement C++11 support).

ad 2: No marking possible, just delete it "manually" on the right place or leave this task on Smart pointers.

ad 3: Not applicable.

Finally, there is always the simplest option - do not allocate your objects on the heap, which means dynamically. In Java there is no such possibility, but in the C++ there is. I have even read in some of Stroustrup's (the creator of C++) great books on C++ programming that in the time of C++ creation such dynamic allocation was not recommended. He stated: to have RAII working correctly, there must not be a dynamic allocation - It sounds strange today but that's what Stroustrup wrote, it is not from my head, I personally allocate dynamically almost everything as everybody does...

The main reason for the static allocation is that the objects get deleted once there are out of scope, so one has not to be worry about exception safety and cleanup at all. If you allocate instance dynamically it is not deleted automatically if the instance leave the current scope - you have a memory leak - if you do not delete the instance manually. Consider the simple try-catch block:

try 
{
Class *instance = new Class;
//some error
}
catch(...)
{
//error caught - current execution is terminated immediately, instance is no deleted - memory leak.
}

I Java there is a finally statement which is always called, so that you can perform necessary cleanup when exception thrown. But in C++ you are in trouble...unless you use mentioned smart pointers or some very similar technique.When using smart pointers you don't have to worry about cleanup any more (not exactly true in practice, but your life will be definitely easier and your code less buggy).

Upvotes: 1

Suzan Cioc
Suzan Cioc

Reputation: 30127

There is no garbage collector in C++. You should write and run destructors on your own. In C++ it is a common error to forget to run a destructor.

If your object is allocated with new, then you should delete it with delete. So, new calls contructor, while delete calls destructor.

myclass *p = new myclass();
// do something
delete p;

This is called dynamic object allocation.

If your object is defined "normally", it will be automatically destructed when out of scope.

myclass a;
// do something
// will destructed when }

This is called automatic object allocation.

P.S. You was also not to assign nulls in Java since garbage collector was invented namely for not to bother with object deletion.

Upvotes: 3

Luchian Grigore
Luchian Grigore

Reputation: 258638

1) If your objects are in automatic storage, you can limit their scope:

{
   X x;
   //...
}  //x gets destructed here

If in dynamic storage, you delete them when you're done:

X* x = new X;
//...
delete x; //x gets destructed

2) You can't (at least in a clean way). You must instruct C++ when to delete your objects, even if that instruction consists of an ending bracket. (see the first code snippet)

3) There is no garbage collector in C++. See the two snippets. You either have to explicitly delete the objects (if in dynamic storage) or they will be deleted automagically (but not by the garbage collector) if in automatic storage.

Something that is worth looking into is smart pointers (there are tons of implementations out there), but that's also not a garbage collector. It just saves you the hassle of managing memory. But it's nothing like Java.

Upvotes: 6

Related Questions