• Advertisement
Sign in to follow this  

Automatic C++ Garbage Collection

This topic is 3384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been looking into C# as a language and the only real thing that attracts me to it over C++ is the automatic garbage collection. I was wondering if there are any c++ automatic garbage collection system out there that i would be able to easily insert into any project and not have to have me deal with managing my programs memory manually?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by 3dmodelerguy
I have been looking into C# as a language and the only real thing that attracts me to it over C++ is the automatic garbage collection.
You need to look closer, then. And beware of the Blub paradox.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sneftel
Quote:
Original post by 3dmodelerguy
I have been looking into C# as a language and the only real thing that attracts me to it over C++ is the automatic garbage collection.
You need to look closer, then.


What do you mean?

Share this post


Link to post
Share on other sites
The STL (Standard Template Library) includes many so called 'containers' where one can store data dynamically and safely in (usually) a very optimized way.

The STL is powerful, but alas, is also a BIG topic too.

Then there is Boost of course. Huge. Smart. Useful. The highly generic, very reusable, easy to integrate code should eliminate you having to mess around with memory directly, unless you really want to.

Don't knock C++ on this small matter of automatic garbage collection. Sure, knock it around on other points if you wish to do so, but in C++, if you know what you're doing, or indeed just simply use the highly professional (and FREE!) libraries that I've just mentioned, automatic garbage collection can be easily implemented at will in C++.

At the moment it's just not... Well... Automatic by default. This ain’t a bad thing, since you can then use whatever automatic garbage collection system which suits your particular project.

Share this post


Link to post
Share on other sites
C# is a very, very rich language - especially by comparison to C++.

For example, learn about delegates and play around with them a bit. Learn about LINQ, especially if you're working against a traditional relational database or something similar. Learn about the .Net framework and library, which in itself is a massive improvement over the C++ standard library in terms of what it can do out of the box. (Yes, you can technically use the .Net library from C++ via C++/CLR, but it's a pain in the ass and doing it right can be hard. C# does it natively with ease.)

You'd also do well to poke into things like C#'s type system vs. the weak typing in C++; this can be a big advantage. There's a huge number of features that aren't major features but make code writing much easier - partial classes, the var keyword, the side-by-side DLL versioning system (which you get for free in C# but have to fight through "DLL hell" in C++ usually), simple interop with COM libraries...

I'm sure there are plenty of others, but to be honest I don't do much C# so I can't name them all off the top of my head.


C# is a gold mine. If all you can find different from C++ to C# is memory management, do yourself a favor and explore C# more - a lot more.

Share this post


Link to post
Share on other sites
Garbage collection is no silver bullet.
RAII is a much more robust way to handle resources, while it makes your code a bit more constrained.

Share this post


Link to post
Share on other sites
Quote:
Original post by 3dmodelerguy
Quote:
Original post by Sneftel
Quote:
Original post by 3dmodelerguy
I have been looking into C# as a language and the only real thing that attracts me to it over C++ is the automatic garbage collection.
You need to look closer, then.


What do you mean?

I mean, someone who is primarily familiar with C++, and thinks in C++, won't immediately be attracted by most C# features. They'll like the idea of garbage collection because the annoyance of manual memory management is always fresh in their minds, but things like delegates, LINQ, lambda expressions, generators... these are weird and not immediately "attractive" features. To the monolingual C++ programmer, C# looks like C++ with garbage collection and various hairy not-very-attractive features thrown in. The solution to this is to learn to think in higher level languages.

Share this post


Link to post
Share on other sites
Quote:
They'll like the idea of garbage collection because the annoyance of manual memory management is always fresh in their minds

Absolutely not. Most C++ programmers avoid garbage collection because they know it's only good for some things, which are quite rare in practice.
They use RAII, which guarantees exception-safe code with no leaks whatever exception may be risen. There is no such thing as manual memory management, everything is scope bound in C++.

Quote:
but things like delegates, LINQ, lambda expressions, generators... these are weird and not immediately "attractive" features.

That kind of features are being used extensively in C++, and it is actually recommended to use them.

Really, I wonder: what kind of C++ have you been using? Probably C with classes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement