Yet another garbage gollection question

This is a discussion on Yet another garbage gollection question within the C++ Programming forums, part of the General Programming Boards category; So there is manual memory management and few useful reference counting classes. Is there any chance to use garbage collector ...

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Yet another garbage gollection question

    So there is manual memory management and few useful reference counting classes.

    Is there any chance to use garbage collector in C++?

    Is there something that auto_ptr or shared_ptr can't handle other than the cyclic reference problem?
    Just GET it OFF out my mind!!

  2. #2
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    there are a few tools for C++ that do automatic garbage collection.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    No, no, I meant: Should we use GC while there is smart pointers?

    Sorry for my crappy English.
    Just GET it OFF out my mind!!

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    I believe smart pointers do GC which is the whole point. There is more to GC than just dangling pointers though. There are other resources that need GC. Thread handles for instance.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,433
    Garbage collection is generally viewed as something unrelated to smart pointers - in rough terms, GC means that some mechanism automatically releases memory resources that the program is no longer using, but has neglected to release - and does it without specific actions from the program.

    In the context of C++, garbage collectors are usually implemented as some library functions that run, potentially independently of your program but accessing its memory space, and do the job of releasing memory when required while your program is running. The Hans-Boehm collector (at this link) is an example.

    If you properly ensure that all resources you use in your code are released (one way is using smart pointers correctly) there is no need for garbage collection.

    Note also that garbage collectors typically only release memory resources - if your program grabs some other system resource (eg a mutex, a file handle) and forgets to release it, then that resource is still leaked. Since those other resources are often more scarce than memory, such leaks are deadly to functioning of programs - and the most common source of bugs I've encountered in code that relies on the presence of a garbage collector.
    Right 98% of the time, and don't care about the other 3%.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    Quote Originally Posted by audinue View Post
    Is there something that auto_ptr or shared_ptr can't handle other than the cyclic reference problem?
    Nothing that comes to mind. They are very handy in releasing handles, too, by calling a custom deleter function (well, at least shared_ptr; dunno if auto_ptr has such a function).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    So, Is GC useless technology ever made for C++?
    Just GET it OFF out my mind!!

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,772
    GC isn't useless technology, but it isn't that suited for C++.
    It's better suited for other languages that are built around it.

    I would not recommend a GC for C++. Especially since a GC 99% of the time lacks deterministic destructors and some may not even run destructors at all.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    Garbage collection is perhaps like have a park with no bins where several people are employed to go around picking up all the rubbish off the ground.
    In the C++ park we have lots of bins, and smart pointers are the tidy people.

    Okay there goes my lame attempt at humour for the day...
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    GC technology seems better with non-machine code languages, ie. runtime languages. I wouldn't know where to begin writing a GC for C++, besides some sort of program similar to a compiler which analyzes all the code, and then generates itself. Which would be strange, and flawed anyway. Proper use of memory and debugging seems to be the best option for C++.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  11. #11
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,248
    Quote Originally Posted by Dae View Post
    GC technology seems better with non-machine code languages, ie. runtime languages. I wouldn't know where to begin writing a GC for C++, besides some sort of program similar to a compiler which analyzes all the code, and then generates itself. Which would be strange, and flawed anyway. Proper use of memory and debugging seems to be the best option for C++.
    Imagine that you replace all bare pointers with this class:

    Code:
    template < typename T >
    class TaggedPtr
    {
    public:
        TaggedPtr( T *ptr ) : mPtr( ptr ), mSig( 0xDEADBEEF ), mCheck( 0xDEADBEEF ^ mPtr )
        {}
    
    private:
        T *mPtr;
        T *mSig;
        T *mCheck;
    };
    (Of course, all the surrounding machinery of your typical smart pointer has been omitted).

    Now, you can easily scan memory to find active pointers. For each three adjacent values x0, x1, x2 in memory, you check whether x1 == 0xDEADBEEF && x2 == x1 ^ x0. If so, you have found a pointer.

    Now that we have a method of finding pointers we need a way of determining the extent of any given object. We can do that by inheriting all classes from a base class which provides a virtual method. Now, the first byte pointed to by any pointer will be a pointer to a vtable, and we can now use RTTI to find the type of the object and therefore its size.

    A complication arises because it's possible to have pointers that point to a location WITHIN some other object instead of to the beginning of that object. This is not handled under this model.

    Other garbage collectors are more conservative, and simply assume that any 32-bit quantity which would be a valid pointer into the current address space, IS in fact a pointer. This might lead to some objects not being collected which could actually be collected, but that's what "conservative" means anyway.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Garbage Collector
    By audinue in forum C++ Programming
    Replies: 27
    Last Post: 08-16-2009, 07:20 AM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. garbage collection
    By joed in forum C Programming
    Replies: 4
    Last Post: 04-01-2004, 12:47 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21