r/learnprogramming • u/Kadabrium • 23d ago
What language is most like a garbage collected version of c++?
I like c++ for its flexible multi paradigm support, wide applications, a decent amount of syntax sugar and a good type and metaprogramming system, but to be fair super precise manual memory management is overkill for most oft the programs i'm going to write.
43
Upvotes
38
u/podule-games 23d ago
C# probably.
It's not that much like C++ in the grand scheme of things, but probably the closest you'll get with GC.
Rust doesn't have GC as such*, but it does mostly manage memory for you, just in a different way.
*It does have reference counting for some things, which some would call GC and some wouldn't, but basically, it's not GC in the Java or C# sense.
7
u/Chad_Frost 23d ago
I'd guess something like golang is the most practical answer. Iirc C# allows for some more manual stuff like C++ compared to java but it runs on the CLR so it's not quite comparable.
If you want to be technical I guess D is the closest one on paper since its basically designed to be C but with GC, but D doesn't really have the same breadth of practical application as something like golang.
17
u/rupertavery64 23d ago edited 23d ago
I'm surprised that despite C# being the primary suggestion here, you don't seem interested in it at all, despite it being most like C++ in syntax, some delicious syntax sugar and yes, elegant type and metaprogramming.
Wait until you learn about switch expressions, try/catch expressions, property initializers, the
Disposablepattern and theusingstatement, extension methods (recentlty expanded to extension members)Let's not forget the versatility of
IEnumerableandIQueryableand how it is powered by LINQ and how that lets you write typed queries against objects or build (SQL) expressions from them, or use however you like. You can even write code in code. And compile it.A rich type and metaprogramming system, with reified generics, an attribute system and reflection.
And of course it is garbage collected, but you can do
unsafestuff with it as well. It exposes safe memory access withSpan<T>andMemory<T>each with language embedded restrictions that guarantee safe usage.C# also introduced
async/awaitwhich really simplifies asynchronous programming and has been making its rounds with other programming languages since.What kinds of programs are you planning on writing?
22
u/amazing_rando 23d ago edited 23d ago
Shared and smart pointers with reference counting and automatic freeing have been in the C++ standard library for 15 years now and should give you all the automatic memory management you need. Modern C++ does not require precise memory management, manual allocation and deallocation, or raw pointers. You need to be aware of your objects' lifecycles but this is also true in garbage collected languages, it mostly just changes the way it breaks if you 're wrong.
31
u/burlingk 23d ago
Java is a contender. Go and Rust are not as syntactically similar.
14
u/11fdriver 23d ago
Here to go rogue as usual. Dlang for me, hands down.
https://dlang.org/
3
u/SnugglyCoderGuy 23d ago
Go. I imagine some would say Rust as well. I use Go professionally and it's easy to get shit done.
3
u/TheSkiGeek 23d ago
Java or C#.
Or C++ and only ever allocate via std::shared_ptr
2
u/gebbles1 23d ago
I maintain a static typed, object-oriented language that's easy to learn, but it's a stack VM based interpreter not a compiler. That said, it's a cool little project and a nice stepping-stone if you're getting into programming and supports a very wide range of features like generics, operator overloading, reflection, aspect-oriented programming, function overloading and much more. It's called Geblang.
2
u/Prestigious-Bet-6534 23d ago
Dlang! You get a modern module system for free too, no more headers or forward declarations.
2
u/gm310509 23d ago
For me I would suggest Java. I came back to C++ after learning Java (I didn't see the point of C++ until I choose to learn Java because prior to that I could do everything it offered C and I could not see the point of all the extra syntax at that time).
As for memory management generally, you typically don't even need to think about it in Java - Java uses a system of references which are pretty much implemented as double pointers (the pointer you have in your code is really just a pointer to a lookup table which has the pointer to the object). The GC can use this fact to move stuff around to close holes in the heap).
In the decades I've been using Java I've never had any problem with it's memory management - and I've loaded up some pretty massive structures into it. To be fair this is the only problem I have had, the Java runtime will allocate a default amount of memory for the heap - usually it is enough, but for the programs I am referring to here, I often had to specify my own maximums.
Some will argue performance - again, this was never an issue for me despite using large in-memory structures where there was quite a bit of allocation and freeing of dynamically created objects.
1
u/Jim-Jones 23d ago
Not C#??
1
u/goldscurvy 23d ago
This is pretty much what java was defined for. The metaprogramming is not nearly as robust or supported. But it is what you are asking for.
Java also has the benefit of being widely supported, having a huge ecosystem, and being incredibly mature.
1
u/NYU_VM 23d ago
Funny enough C++11 had introduced optional GC but no-one adopted and it just phased out
1
u/pixel293 23d ago
I would think with C++ and shared/unique pointers you wouldn't need to worry about freeing memory, it just happens. And unless you are doing new/delete in a tight loop the overhead isn't going to be noticeable. But if you want another language, Java would be the closest to C++ syntactically.
1
u/kevinossia 23d ago
Well it’s a good thing C++ doesn’t require super precise memory management, then.
C++ uses reference-counted garbage collection just like Swift or Rust. Try it out. It’s not hard, I promise.
1
u/Comprehensive_Mud803 23d ago
C++ in a trash bag?
1
u/LukaRozman27 23d ago
C is probably the closest, especially since.NET Core 3.1 or newer, but you lose C++ style templates for generics and get a runtime instead of native binaries. The syntax and multi-paradigm support are pretty similar though.
1
u/green_meklar 23d ago
Java is pretty close, isn't it?
1
u/vegan_antitheist 23d ago
You can just use a GC for C++. There's cpp-gc.
1
u/Ok-Pianist9692 23d ago
Honestly, I'd give C# a serious look.
Coming from C++, it feels familiar enough that you won't feel like you're learning programming all over again. You still get OOP, generics, operator overloading, LINQ, reflection and a pretty powerful type system, without worrying about manually managing memory.
The ecosystem is also mature enough to build almost anything.
I'd say C# is probably your best practical starting point. D is interesting if you specifically want something closer to C++ in philosophy.
1
u/OutlandishnessNo8034 22d ago
Rust
1
u/ComasArentFun 21d ago
C# is the literal answer to this question
Every bit of the best syntactic sugar and library support you could want, with the backing of a comprehensive GC process
I've not though about low level memory optimisation in over a decade.. just potential runtime memory leaks
Let something dangle, the GC tidies it up
1
u/KroniklyOnline 21d ago
Rust....
1
u/Efficient-Scheme1995 21d ago
try golang++
1
u/SpaceAviator1999 20d ago edited 15d ago
It sounds like what you're looking for is provided in C++11.
Try to avoid using pointers -- by declaring as many variables as you can on the stack -- and that way you won't really need garbage collection.
But in those times that you need pointers, instead of using the old pointer declarations that will require manual deletion, like this:
use
std::shared_ptrinstead (provided by the#include <memory>header):So you see, replacing the old
MyClass *ptr = new MyClass(...)declarations with the C++11auto ptr = std::make_shared<MyClass>(...)declarations will automatically get your memory garbage-collected for you, even when you create another shared pointer and set it equal to an earlier shared pointer!1
u/beastfacebasterd 20d ago
C#
0
u/BranchLatter4294 23d ago
Rust, C#, etc.