Thread: Class method overloading

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Based on his online FAQ answers, I daresay Stroustrup thinks that garbage collection does have its uses in C++, at least as a last resort. Consider the last paragraph of his answer to How do I deal with memory leaks?:
    Quote Originally Posted by Bjarne Stroustrup
    If systematic application of these techniques is not possible in your environment (you have to use code from elsewhere, part of your program was written by Neanderthals, etc.), be sure to use a memory leak detector as part of your standard development procedure, or plug in a garbage collector.
    He does not condemn garbage collection, but states in his answer to Why doesn't C++ have garbage collection?:
    Quote Originally Posted by Bjarne Stroustrup
    If you want automatic garbage collection, there are good commercial and public-domain garbage collectors for C++. For applications where garbage collection is suitable, C++ is an excellent garbage collected language with a performance that compares favorably with other garbage collected languages.
    That FAQ links to Hans-J. Boehm's GC implementation that SevenThunders mentioned.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    part of your program was written by Neanderthals
    I run into this problem far too often.

  3. #18
    Registered User
    Join Date
    Apr 2007
    Posts
    141
    Quote Originally Posted by Elysia View Post
    No, it's not. It's much faster than a GC which has to scan through all pointers and check their ties and see if it can free them. So no, smart pointers are much faster.
    You are forgetting the fact that you have the overhead of incrementing the reference counter on every assignment, and decrement it for every delete, not to mention the fact that you have to keep overhead around for reference counting.

    No! You're going about this the wrong way! Use smart pointers to deal with the data. They will ensure that the data will only be deleted when there's no reference to them still. No problems there.
    So you have smart pointers that are reference counted. Again that's just a form of garbage collection! Generational garbage collection is considerably more efficient and universal then reference counting. Reference counting will fail in the presence of cycles, which occur quite often in graph theory.


    The problem with a GC is that either it runs as another thread, in which case it needs to lock access to pointers (or a global operation), which means additional overhead each time your program accesses a pointer. Or it can run at specified intervals, blocking execution of your program while it does its work.
    Freeing a lot of small objects manually or when a sizable function returns also 'blocks execution'.


    And compacting has its own problems. If it moves memory around, it needs to update all pointers! Lots of work.
    It's just slower than manually freeing memory.
    I agree that compacting is not really backwards compatible with C++, but it does reduce memory fragmentation and improves cache performance.

    Any way this debate has been going on for awhile. Here are some interesting discussions about garbage collectors from some experts.
    http://www.hpl.hp.com/personal/Hans_...gc/issues.html
    http://www.digitalmars.com/d/2.0/garbage.html
    http://www.ocaml-tutorial.org/garbage_collection

  4. #19
    Banned
    Join Date
    Nov 2007
    Posts
    678
    if a slow language like java can afford overhead of GC, then why a super fast language like C++ has problems with it being inefficient???
    Last edited by manav; 03-25-2008 at 02:49 AM.

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by SevenThunders View Post
    You are forgetting the fact that you have the overhead of incrementing the reference counter on every assignment, and decrement it for every delete, not to mention the fact that you have to keep overhead around for reference counting.
    This dwarfs compared to what a Gabage Collector needs to do.
    I tried writing one before but gave up due an unsuitable API for the job.
    A GC needs to keep lists in memory about every allocation, and in case in allocates in, like, said pages, that means even more overhead.
    And does it need to keep check on how many references? Yep.
    So a GC's overhead is far more. Not to mention the time it takes to go through all those lists, create them and manage them.

    So you have smart pointers that are reference counted. Again that's just a form of garbage collection! Generational garbage collection is considerably more efficient and universal then reference counting. Reference counting will fail in the presence of cycles, which occur quite often in graph theory.
    It is not "just another...". A very light reference counting solution needs to reduce reference count, check if zero, and if yes, delete. And the other way around for when incrementing.
    But unlike garbage collection, this is a relatively low overhead.

    Freeing a lot of small objects manually or when a sizable function returns also 'blocks execution'.
    Yes it does, but it usually dwarfs how much the GC has to free at once time.
    And when it does, it needs to go through checking what to free. If the application frees it explicitly, it will always be faster. But freeing a lot of memory is always slow, as you mention. But the application will be faster then the GC.

    I agree that compacting is not really backwards compatible with C++, but it does reduce memory fragmentation and improves cache performance.
    This is one the main advantages you might say. But then again, Windows also has low-fragmentation heaps, which can do the job for us.

    Quote Originally Posted by manav View Post
    if a slow language like java can afford overhead of GC, the why a super fast language like C++ has problems with it being inefficient???
    Two reasons I can think of:
    Destructors are not guaranteed to be called at end of scopes or functions.
    GC is a bigger overhead, more complex to manage and use and C++ programmer have pretty much always wanted to control their own memory. The language was built that way from the start, after 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.

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by manav View Post
    if a slow language like java can afford overhead of GC, then why a super fast language like C++ has problems with it being inefficient???
    Ever considered that the slowness of Java might, partially, be caused _BY_ the GC. In fact, as I understand it, C++ was invented specifically to avoid the overhead of GC in a simulation program written in Simulua 67 [another language that does GC], as when Bjarne Stroustrup started the LARGE simulation, the GC processing was 80% of the overall runtime. [This was probably a BAD case of GC implementation, but it's still part of why C++ was implemented in the first place].

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling a class method within different class method
    By alyeska in forum C++ Programming
    Replies: 5
    Last Post: 03-08-2009, 10:56 AM
  2. Windows Service class constructor and OnStart method
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 04-14-2008, 07:18 AM
  3. Overloading an "External" Class?
    By Zeusbwr in forum C# Programming
    Replies: 2
    Last Post: 10-30-2005, 04:34 PM
  4. problem with sending files to a class method..
    By utoots in forum C++ Programming
    Replies: 5
    Last Post: 04-02-2003, 01:38 AM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM