garbage collection enabled c++

This is a discussion on garbage collection enabled c++ within the C++ Programming forums, part of the General Programming Boards category; since i am not very comfortable with c++ way of manually managing the dynamic memory. i was thinking if there ...

  1. #1
    Banned
    Join Date
    Nov 2007
    Posts
    678

    garbage collection enabled c++

    since i am not very comfortable with c++ way of manually managing the dynamic memory. i was
    thinking if there is a c++ compiler or something like that that would provide me garbage collection
    in c++ without any extra efforts in coding on my part.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    I suggest that you read Stroustrup's answer to the FAQ: How do I deal with memory leaks?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There are two things to consider here:
    1. C++ was originally created by Bjarne Stroustrup because the garbage collection in Simula-67 that he originally wrote the code in was taking some 80% of the CPU time when running a simulation of some sort. Hence he rewrote the Simula code using a special parser that created standard C code - this later became "Cfront", which was the first C++ "compiler".

    2. There are ways to avoid quite a bit of manual freeing of data, for example using Smart Pointer classes.

    Or you could use Java or C# - both of these languages have gc as part of the language.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,782
    I would go with smart pointers. Never have to worry about manually freeing memory again.
    That's the beauty and power of C++ - if there's something you need, you can almost always emulate such behavior with classes.
    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.

  5. #5
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks laserlight and mats!
    i had almost made up my mind to switch to java. i downloaded the jdk6. ran the programs. and boy!
    they were so slow i could take a nap in between (ok, that's too much, sorry!).
    since all of java is byte code. but all of c++ is native code. so a gc will not affect much in c++ case.
    i have read that article.
    in my project there are 1147 new and 447 delete. >_<
    and they can't be avoided (not easy, nope! *_*).
    may be there is some compiler support for gc. does g++ do it?

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,782
    Use smart pointers. There is no "garbage collection enabled" compiler for C++.
    Last edited by Elysia; 02-13-2008 at 06:17 AM.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,914
    in my project there are 1147 new and 447 delete. >_<
    and they can't be avoided (not easy, nope! *_*).
    Well, that you have less delete than new means that somewhere, the use of delete is being hidden (assuming that you are not simply failing to match new with delete).

    So yes, they can be avoided (the deletes, at least), though if you did not write the code with RAII in mind, it may not be trivial to convert.

    Use smart pointers. There is no garbage collection for C++.
    Stroustrup himself points out that garbage collection libraries are available for C++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not aware of any gc capable C++ compatible compiler.

    Having an unequal number of new and delete is, in itself, not a problem, consider this:
    Code:
       if (a) 
          b = new base;
       else
          b = new derived;
       ...
       delete b
    But you should of course delete every object you create, and in large projects, this CAN be difficult - but encapsulating the new/delete in classes, that is a way to handle it.

    Another way is to have your own new/delete functions [for debug purposes], and write some extra code in places to check if you have more new than delete when you expect the code to be "neutral", so for example call the function that builds a binary tree from an input file, then call the teardown of the same function and check that you have the same number of allocated objects.

    Yes, it's extra work, but it's not an insurmountable task to achieve this.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks elysia!
    Code:
    truct Shoe { ~Shoe() { std::cout << "Buckle my shoe\n"; } };
    
    int main()
    {
        boost::scoped_ptr<Shoe> x(new Shoe);
        return 0;
    }
    i found this example on boost. can i do here this instead?
    boost::scoped_prt<Shoe> x = new Shoe;
    this is more clean syntax imo. is there an error using this syntax?

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,782
    Only scoped_ptr will delete the pointer when it goes out of scope. You're looking for something like shared_ptr I think.
    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.

  11. #11

  12. #12
    Banned
    Join Date
    Nov 2007
    Posts
    678
    thanks pheres! i think this is what i wanted! they saay it's a bit harder to use it for c++. but i hope it's not too
    much work. otherwise i have no problem discarding the use of free/malloc and use GC_MALLOC/GC_FREE instead!

  13. #13
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    I do think you should give boost.smart_ptr library a try. It solves most (not all) of the problems you may encounter with memory allocation and, it seems to me, more easy to use than the also famous hans bohem garbage collector (libgc). Part of this boost library was also added to the TR1, which may mean an inclusion on the upcoming standard library. This will allow you to use a library that shares a commnon semantic background with the standard library.

    I think it would be good for you to also implement your own smart pointers to better understand some of the issues. Reference counting in a particular is a powerful technique - and quite an interesting one too - since it will give you ideas for many other things not smart-pointers related. I know it has to me.

    The best description of reference counting I've seen so far was in the book C++ Primer, 4th Edition from Addison Wesley. It explain the technique in an easy to use language and in good detail. If you can't afford to get a new book right now... search google for "C++ RAII" and "C++ reference count". There is bound to exist a good tutorial on the technique.

    Posting here as you try to implement your own reference count class will earn you extra miles in the knowledge you will acquire.

    Really, I do think RAII is one of the most important things one should learn about when programming in C++. It's da Design Pattern one should learn before learning any other. Many book authors ignore the subject almost entirely, and yet this is one of the fundamental concepts of the language. In won't be enough to just use someone else's garbage collector (be it boost or libgc).
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  14. #14
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    The main problem with GC is that you lose deterministic destruction. In C++, many classes rely on deterministic destruction for timely cleanup of resources.

    Boehm's GC library doesn't call destructors at all, I think. Read this:
    http://www.hpl.hp.com/personal/Hans_...alization.html
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  15. #15
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,510
    That reminds me reading a short time ago about that being a common problem of GC libraries. An article by Herb Sutter, if memory serves me right.

    Couldn't follow the article in its entirety (I lack the background knowledge for that), but got the impression this happens because of how finalizers are used to bind destructors to GC, and he advertises this as wrong since, particularly to local objects if they lose scope, we do want them to be destroyed that instant.

    Meanwhile any performance considerations - which seems the real reason behind non-deterministic destruction proponents - holds little value because that performance degradation that was avoided will resurface somewhere else... and it allows us to write code that is conceptually wrong.

    All in all I got the impression that he was saying GC is really not for C++. We should stick to deterministic destruction.

    EDIT: If possible, I would like to know a little more about this
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Garbage Collection is not so complicated
    By Nalpdii in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 11:34 PM
  2. C++ Garbage Collection
    By vaibhav in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2005, 09:26 AM
  3. Garbage Collection
    By Orborde in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2005, 11:18 PM
  4. SDL: Garbage collection (?) screwing me
    By Brian in forum Game Programming
    Replies: 4
    Last Post: 05-08-2005, 09:13 AM
  5. garbage collection
    By joed in forum C Programming
    Replies: 4
    Last Post: 04-01-2004, 12:47 PM

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