14
donuts
7y

So in C++ there is no garbage collector to clean up the mess u leave behind?

And now i feel like a garbage collector.... always cleaning up after monkeys... :(

Comments
  • 6
    If you have access to C++11 you could use the smart pointers (shared_ptr, unique_ptr) introduced in it.
  • 1
    btw... so is there actually a Garbage Collector utility in C++ lib or do u usually just use delete?

    And i guess getting ahead of the book but... why would i ever need to use malloc?

    or is this how you declare arrays?
  • 1
    @jmclemo6 so guess that answered my followup question...

    I have whatever C++ is in VS 2017....
  • 2
    @billgates I would assume you have C++11 support then. I use Vim and g++ by themselves though, so you would have to look it up to make sure. :P
  • 0
    @jmclemo6 well i guess this answers the question... I have nullptr
  • 0
  • 0
    @Artemix ok... So basically this book is teaching everything WRONG.... crap....

    So need to start over again...
  • 0
    @Artemix you are saying to code c++ the way you would code java...
  • 0
    There actually is a pretty good garbage collector for C/C++ called the Boehm GC
    http://www.hboehm.info/gc/
    But since C++ was never meant to be garbage collected it can result in some hard-to-find bugs.
    You can also roll your own GC without much effort (because C++ lets you work with low level memory manipulation). The advantage here is that your GC can be optimized for your application (eg. a GC for a game engine would have different requirements compared to a GC for a simple desktop app).

    However, good C++ coding practices (smart pointers, etc) more-or-less make GCs unnecessary. If you're really scared of memory leaks you may want to switch to a GC-ed language like C# or Java.
  • 0
    @hsbfsix I'm not scared... Just saying this feels new and maybe good... C++ developers all know how to clean up their shit... Unlike a lot of Java developers...
  • 1
    @billgates hey we are occupied with cleaning up enterprise-level shit okay?

    @Artemix yeah my bad, seems like i cant fucking read
  • 1
    @billgates
    Yeah sorry, didn't mean it that way.
    Like I said, what you use should depend on the application.
    And also, welcome to the world of memory management!
  • 0
    If you are very lazy or truly need a garbage collector, you can use Hans boehm garbage collector. I used it to make a Lisp interpreter and it is amazing, but atrociously complicated
Add Comment