Did you like how we did? Rate your experience!

4.5

satisfied

46 votes

Does having a manual garbage collection and memory management make?

Not at all. Very few objects need manual recovery of memory. Only items allocated with operator new and legacy code for interoperation with C code using malloc for dynamic memory allocation need worrying about, as far as making sure a delete or free, respectively, is called. The rest autodelete either by automatically being recocovered when their lifetime ends by going out of scope, or when being replaced by another object such as in an assignment. Classes and structs have their own destructor functions that clean up all the non-static data members.If a class has dynamically allocated storage, then a delete command for that storage in the class destructor will take care of that. So unless you are doing something fancy in real time (run time) with memory allocation, you need not even bother with complex or dicey allocation/deallocations. The lack of a garbage collector allows for tight objects and high speeds without pauses to collect, such as in interpreted languages. There does not need to be a collector thread running at system privileges to keep the system running smooth and fast. In fact in a compiled program with direct memory access a garbage collection scheme would have to be highly complex compared to the small runtime system C++ features. C++ objects are allocated primarily on the stack in the stack frame of the function, and do a fast destructor cycle when they go out of scope, usually when the enclosing loop ends or the function returns. The code portion of a class (its static members, and member function code) is stored in a separate portion of memory and stays active for the life of the program. Only the instance storage is allocated and deallocated, and that is completely recovered when the instance runs its destructor automatically. The STL library has sets of objects that are dynamic such as lists, vectors, queues, strings. These use operator new internally on the free store (the heap) and call the delete operator on their contents recursively when they are destructed. So no user manual deletion has to be written from scratch, it all happens normally when the object goes out of scope.

100%
Loading, please wait...