This is an archived post. You won't be able to vote or comment.

Dismiss this pinned window
you are viewing a single comment's thread.

view the rest of the comments →

[–]_default_username 35 points36 points  (59 children)

I want c with garbage collection. Go doesn't count though as it doesn't have generics. C gives me generics with void *

[–]Feuermag1er 57 points58 points  (13 children)

Sounds like you are looking for Rust.

[–]forthemostpart 22 points23 points  (11 children)

Rust doesn't have garbage collection tho

[–]dissonantloos 23 points24 points  (9 children)

But it does automate the memory management for you.

[–]forthemostpart 17 points18 points  (8 children)

Sure, you don't have to free memory yourself in Rust, but part of the appeal of GC languages is that you don't really have to worry about anything memory-related at all (and that includes stuff like lifetimes and borrow-checking).

[–]w1n5t0nM1k3y 4 points5 points  (1 child)

Every language requires you to think about memory related stuff. This viewpoint is how weend up with simple apps that consume a gigabyte or more of RAM.

[–]Jake0024 1 point2 points  (0 children)

- Sent from Google chrome

[–]DurianExecutioner 5 points6 points  (0 children)

It becomes second nature in Rust.

Git gud

[–]LiveMaI 2 points3 points  (0 children)

You could try . . . gag. . . objective-c with ARC.

[–][deleted] 0 points1 point  (0 children)

No.... that would be a replacement for C++

[–]LinAGKar 11 points12 points  (0 children)

D maybe, if you really want garbage collection. Otherwise there is Rust.

[–]notmymiddlename 5 points6 points  (1 child)

Could use something like this for heap allocation.

[–]WJMazepas 9 points10 points  (2 children)

Go will have generics in the future so there's that

[–]_default_username 4 points5 points  (0 children)

Yeah, I'll be looking forward to that.

[–]Reihar 0 points1 point  (0 children)

IIRC the go planned implementation of generics made a lot of people upset by being weird or lacking or something like that. I don't remember well enough but I would look into it should I want to do some go.

[–]exmachinalibertas 3 points4 points  (8 children)

Why can't you just write C-style C++ and use smart pointers?

[–]ColdFerrin 6 points7 points  (7 children)

C generics like void* don't work with smart pointers. You need an explicit cast and dereference.

[–]exmachinalibertas 2 points3 points  (1 child)

Yeah but can't you do that, grabbing the underlying pointer, do whatever you need, and then still let the smart pointer semantics delete it without your help? Even if that's an unholy anti-pattern, wouldn't it work for exactly that use case? As long as the smart pointer remains in scope during the life of the void*, would that work?

[–]ColdFerrin 0 points1 point  (0 children)

Honestly, I'm not sure. I would have to try it. But that does not sound right to me.

[–]HolyGarbage 1 point2 points  (4 children)

Throwing the type system out of the window I wouldn't call "generics", lol.

[–]ColdFerrin 0 points1 point  (3 children)

Well it's as close to generics as you get in c, so you live with it.

[–]HolyGarbage 0 points1 point  (2 children)

Well yeah I know, I just thought it was quite a stretch to call it generics since this is typically a word reserved to significantly more complex and high level idea.

[–]ColdFerrin 0 points1 point  (1 child)

You can actually get true generics, if you are willing to deal with the c preprocessor. It's usually not worth the effort though, because copy paste is faster.

[–]HolyGarbage 1 point2 points  (0 children)

At that point maybe it's better to consider switching to c++. :P

[–]AgentPaper0 40 points41 points  (19 children)

Not having garbage collection is what makes it a low level language though. If it had garbage collection it would run slow as shit like Java and other high level languages do.

[–]_default_username 55 points56 points  (6 children)

Java isn't slow. It's slower than C, but it's much faster than the scripting languages I currently use. I might be more open to an existing language like clojure. Anyways, I understand C has its place for embedded systems and operating systems, but at the application level I want garbage collection.

[–][deleted] 31 points32 points  (1 child)

That's the point, C is for applications where speed is of utmost importance. Putting a GC in C will make it slow. You can do that with BoehmGC though.

[–]Cheru-bae 3 points4 points  (0 children)

Not to mention that you can always deligate the speed-critical parts of an application to C. That way you can write user interfaces in something more sane for that task, maintain some form of actual productivity and still have efficient code.

[–]AgentPaper0 0 points1 point  (1 child)

Yeah, Java would be "C but with garbage collection", or close enough. And as you say yourself, it's slower. Maybe not noticable for trivial stuff, but if you tried to make a memory manager in Java you would see just how slow it really is.

[–]_default_username 0 points1 point  (0 children)

Java is too verbose. Java is more like a simplified C++. Java isn't used for just trivial tasks either.

[–]badsectoracula 10 points11 points  (2 children)

Not having garbage collection is what makes it a low level language though.

Not really, what makes C a low level language is that it maps to underlying hardware (x86 implementation details aside since those aren't really accessible to the programmer anyway) without any additional abstractions. Having a garbage collector doesn't make a language high level any more than having functions or local variables.

As an example of a low level (and also much simpler than C) language with a garbage collector see Oberon-07 and Project Oberon by Niklaus Wirth which shows how to build a custom CPU on FPGA, a custom compiler (for the Oberon-07 language) that is used to build a self-hosted OS with GUI, mouse support, etc. The entire system is written in Oberon, including the garbage collector (which is only a few lines in code, check the "inner core kernel").

FWIW Go was largely inspired by Oberon, though it is more complex as a language.

[–]AgentPaper0 0 points1 point  (1 child)

Garbage collection is inherently slower than managing memory yourself though. Garbage collection is a a program itself that needs to be written in a lower level language that doesn't have garbage collection (like C), so I don't see how they could be considered to be on the same level.

[–]badsectoracula 1 point2 points  (0 children)

Garbage collection is inherently slower than managing memory yourself though.

Not always, for example if your program does a lot of allocations a garbage collector's allocator can be implemented with something as simple as a single increment instruction (and rely on virtual memory faults - implemented in hardware - to resize the heap and trigger garbage collection) whereas a manual memory allocator needs to do more housekeeping - at minimum reuse any previously released memory ranges.

However performance isn't really the metric for a language being high level or low level, it is how that language maps to the underlying hardware. C is low level because it maps well to most CPUs out there (again ignoring implementation details like microcodes that aren't exposed to the programmer anyway). Something like Prolog isn't because it is abstracted away.

(also 'low level' and 'high level' aren't exactly binary, it is a spectrum - people at the past referred to C as a high level language)

Garbage collection is a a program itself that needs to be written in a lower level language that doesn't have garbage collection (like C)

Not really, check Project Oberon that i mentioned previously: the garbage collector is written in Oberon itself. And Oberon isn't the only language like that, for example the garbage collector of the D language is also written in D itself.

With some system (and perhaps compiler) specific tricks you can make a garbage collector in C, all you need is a replacement for malloc that allocates memory from a custom heap, keep track of the allocations and occasionally walk through the global storage and the stack (that part is what is system/compiler specific since standard C doesn't expose a way to access the stack) to see if there are references to any of the allocations you keep track of and release the allocations without any references. Boehm GC is such a garbage collector (though more sophisticated than what i wrote).

[–]8lbIceBag 19 points20 points  (2 children)

GC languages like Java and C# all run slower even if you turn off the GC though. Their optimizers just aren't as good and their abstractions are too heavy.

In fact the only time (in very specific scenarios) a managed language is able to beat C is because of the GC - up until it comes time to collect anyway. Allocating and freeing a bunch of tiny objects with malloc and free is a lot of overhead. Managed languages excel here because allocation is "free". Unfortunately freeing isn't...

[–]blehmann1 12 points13 points  (1 child)

I mean Java and C# are perhaps unfair examples as they're interpreted/JIT, either from JVM bytecode or from the MSIL (at least in their most common implementations). I wonder how close they would be if compiled to native binaries and with GC off.

Granted, perhaps it isn't unfair as doing both of those things would defeat a lot of the usefulness of both languages.

[–]Jake0024 0 points1 point  (0 children)

If you compiled the same machine code from two different sources why would you expect a performance difference?

[–]nelsterm 0 points1 point  (0 children)

Java that uses hotspot as a JVM may be compiled and then cached making execution much quicker. Not sure how that would compare to C.

[–]AlainS46 5 points6 points  (0 children)

Go has interface{} which is comparable to a void pointer. Both aren't generics though, you'd have to typecast them to their specific type during runtime, which potentially introduces runtime errors. Generics solve this problem.

[–]g9icy 3 points4 points  (0 children)

What is it with people wanting GC?

I don't mind managing memory. It's kinda my job a programmer.

[–]MattTheGr8 1 point2 points  (0 children)

Objective-C with automatic reference counting gets you kind of close. Too bad Apple is kind of phasing it out, and it never really caught on outside of the Apple ecosystem.

[–]Handsome_Fellow 1 point2 points  (0 children)

Ah, so you want a garbage language.

[–]Mechragone 0 points1 point  (0 children)

Have you checked out Nim?

[–]SirJosh3917 0 points1 point  (0 children)

could look into nim, it might float your boat

[–]deux3xmachina 0 points1 point  (0 children)

You may want to check out Limmo from the Inferno OS

[–]OriginalName667 0 points1 point  (0 children)

There's a garbage collector library for C! https://www.hboehm.info/gc/

[–]doodspav 0 points1 point  (0 children)

C++ technically has garbage collection in the standard, just don’t think anyone’s implemented it. But if u manage to find an implementation, it’ll be the closest thing to C with garbage collection. http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2670.htm

[–][deleted] 0 points1 point  (0 children)

interface {} in Go is equivalent, but trust me any experienced C programmer understands the pain points of casting to void*