3

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.

Georg Fritzsche
  • 97,545
  • 26
  • 194
  • 236
dervel
  • 78
  • 3
  • 9
    C++ does not provide garbage collection (you can implement it, but it's a real pain in the ass and usually not needed). It has [RAII](http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization) instead, which makes destructors very useful. –  Feb 13 '12 at 18:01
  • 5
    Find a book. C++ and Java are as different in this area as a car and a train. – Mooing Duck Feb 13 '12 at 18:02
  • 2
    Have you read the article you linked to? C++ doesn't have a garbage collector. Your questions don't make sense. – JB Nizet Feb 13 '12 at 18:03
  • 1
    I second reading a good [introductory book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Georg Fritzsche Feb 13 '12 at 18:04
  • There is no way in Java to force garbage collection. When you call `System.gc()`, it tells the runtime that this would be a good time to start garbage collection, but the GC actually runs on a separate thread at an indeterminate time in the future. – Ted Hopp Feb 13 '12 at 18:08
  • @MooingDuck: And which is which? (going to find some popcorn ;-) – A.H. Feb 13 '12 at 18:10
  • @A.H.: I had actually changed the analogy to something that wouldn't imply that one is better in general, they're simply different. I didn't actually make any associations. (If I had to choose, I'd say C++ is the car. Goes more places, but you have to control it yourself and it's easy to crash and ruin your life.) – Mooing Duck Feb 13 '12 at 18:18

5 Answers5

6

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • +1 for mentioning smart pointers. There is a very good introduction to smart pointers [on Stack Overflow](http://stackoverflow.com/a/106614/1203803). Note that the C++11 standard library has smart pointers so there is no any more need to use Boost for that. –  Feb 13 '12 at 18:19
4

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)

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
3

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.

Suzan Cioc
  • 29,281
  • 63
  • 213
  • 385
  • 1
    Can you change "defined normally" to something more formal? Only you know what that means. – Luchian Grigore Feb 13 '12 at 18:06
  • 1
    Tried to. In C++ you can create object "on heap", which is like in Java. In this case you receive a pointer to an object, which you should pass to `delete` at the end. Another way is "normal", i.e. where objects are created in "by value" manner which is true in Java only for integer types. – Suzan Cioc Feb 13 '12 at 18:09
  • 3
    Heap and stack are implementation details and are not part, in this context, of the C++ nomeclature. The terms are automatic and dynamic storage. There's no "normal" way of allocating objects in C++. – Luchian Grigore Feb 13 '12 at 18:14
  • I asked a question addressing this recently, might want to take a look over it - http://stackoverflow.com/questions/9181782/why-are-the-terms-automatic-and-dynamic-preferred-over-the-terms-stack-and – Luchian Grigore Feb 13 '12 at 18:19
1

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.

Nathan Monteleone
  • 5,430
  • 29
  • 43
  • 3
    Just a nit, but there's no guarantee that when you free memory in C++, it becomes immediately available. I'm aware of systems where freeing memory which was allocated in a different thread is deferred, and there are likely other cases as well. – James Kanze Feb 13 '12 at 18:08
  • And unless you call `delete`, the object will remain permanently allocated, even if it becomes unreachable. – Ted Hopp Feb 13 '12 at 18:10
  • @JamesKanze -- correct me if I'm wrong, but the destructor itself is always called immediately right? As far as when the memory becomes available for future new statements, I imagine that's more implementation-dependent.. – Nathan Monteleone Feb 17 '12 at 18:41
  • @NathanMonteleone The destructor is called immediately. The `operator delete()` function is also called immediately. Whether the `operator delete()` function makes the memory immediately available or not is another question---at least one implementation I'm aware of, for example, uses separate memory pools per thread; if the memory being delete was allocated by a different thread, it simply puts it in a list for treatment later by that thread. – James Kanze Feb 20 '12 at 08:31
1

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).

vitakot
  • 3,786
  • 4
  • 27
  • 59
  • The main C++ compilers (MSVC, Intel, GCC, and Clang) all have some support for C++11, the support varies from compiler to compiler. The new smart pointers are widely supported because they are primarily a library extension. VS 2010, gcc as far back as 4.3 I think, and clang with libc++ all have them. – bames53 Feb 13 '12 at 20:28
  • Also, I typically use very little dynamic allocation, preferring to use automatic storage duration variables. I think you'll find that using dynamic allocation directly is much rarer in C++ than your comment "allocate dynamically almost everything as everybody does" indicates. (and if it's not rare then IMO people are writing C++ wrong). Most instances where one might use dynamic allocation directly I prefer to use a type that will handle it for me instead of doing it directly myself, like `vector` for dynamic arrays. – bames53 Feb 13 '12 at 20:44
  • Thanks for clarification and completion my answer. Actually, I know about the smart pointers support but didn’t want to overcomplicate my answer. And yes, I also use static allocation a lot, probably more than dynamic one. I have read the dynamic allocation looks to be more “objective” and is overused because of that :) But I also don’t think it is necessary a bad practice. – vitakot Feb 13 '12 at 21:00