4 ms·
Zig originally used the same strategy as the D compiler - not freeing memory until process exit wait ... what!? impractical in C/C++ because of language f
by waynecochran 4y ago
Zig originally used the same strategy as the D compiler - not freeing memory until process exit
wait ... what!?
impractical in C/C++ because of language footguns
C and C++ are now very different languages. You might as well say objective-c/swift
- verdagon 4y agoNot freeing memory is a fairly common approach, in compilers and command line tools as well. If an AST hangs around until the end of the program anyway, why not let the OS take care of blasting it away? free() is expensive after all.
- waynecochran 4y agoI am guessing LLVM does not do this.
- JonChesterfield 4y agoFunctions like Type::getInt32() or whatever it's called return the same heap allocated pointer each time so pointer equality can be used for value equality. That's a nice trick that only really works optimally if you leak the pointer, or at least don't free it until very near the end. I've seen cleaning up memory at the end of a program take 20% of the run time and that was indeed patched to just exit & leak as a result. With a flag to clean up so we could still run valgrind on it usefully.
- sanxiyn 4y agoLLVM has JIT users, so LLVM can't do this.
- Arnavion 4y agoYes, any compiler-as-a-library software can't do this. And compilers-as-libraries are becoming more popular these days since being a library makes it easier to integrate the compiler with tooling, like linters and IDEs / language servers.
- hardware2win 4y agoWut? How they were written before if not as library? People didnt write good, modular software "back in the days"?
- Arnavion 4y agoYou say "before" as if it's a thing of the past :) gcc is still not written as a library. clang became famous partly for providing a C compiler as a library. Though gcc's case is specifically because of ideology. Even ignoring ideology, there used to be no need to write compilers as libraries, because the only binary that would use such a library was the compiler itself. The only purpose of the compiler was to be invoked from a shell / build script to compile code. That is changing with things like IDEs that need "compiler services" becoming more mainstream, as I said.
- fooker 4y agoLLVM absolutely does this for storing instructions.
- waynecochran 4y agoDang, I thought it was just my code that was leaky...
- resonious 4y agoForgive me if I'm being a bit presumptuous here, but it feels like "C and C++ have diverged so saying 'C/C++' is now wrong" is now one of those viral sentiments that people just fling around. Like "ah! he said C/C++! gotta call him out". Perhaps that's not what you're doing here, but I think it's completely fair to say that C and C++ have language footguns, even if they have diverged. Objective-C/Swift probably also have footguns ;)
- Conscat 4y agoIf the specific statement meaningfully applies to both C and C++, as it does here, I think it's completely valid to write C/C++. And I say that as a fairly advanced C++ boutique templates author. https://github.com/Cons-Cat/libCat/blob/main/src/libraries/allocator/cat/allocator https://github.com/Cons-Cat/libCat/blob/main/src/libraries/a...
- waynecochran 4y agoThere was a time when C++ was merely "C with Objects." Those days are long gone. Those have been the two primary languages of my long career. The modern C++ I write today bears some similarities, but it like saying Latin/English.
- resonious 4y agoI'm well aware of the explanation. I've seen it numerous of times in almost the exact same wording across HN and elsewhere.
- pjmlp 4y agoFor me those days are only gone on my hobby coding and conference talks. Have some fun reading the code of Windows SDK C++ libraries, Android, or plenty of C++ libraries used in enterprise shops, plugged into managed languages.
- flohofwoe 4y agoI started to use "C/C++" specifically for the bastard-language which is called the 'common subset' of C and C++.
- troutwine 4y ago> wait ... what!? There's whole allocation strategies built around this idea. One of the simpler, more charming ones is a 'bump' allocator. The implementation of malloc bumps an offset in contiguous blob of bytes and free does nothing at all. malloc is very cheap, the OS takes care of dealing with the contiguous blob of bytes. Bump past the end of the blob and your program crashes.
- celrod 4y agoSmarter bump allocator implementations, like the one in llvm, will allocate a new blob instead of bumping past the end of an old one.
- pjmlp 4y agoA/B is an English grammar mechanism to shorten the use of and, yet some people really get pedantic about it meaning anything else.
- flohofwoe 4y ago> wait ... what!? It makes a lot of sense for short-lived command line tools to not free memory, since usually any allocated items will be needer over one invocation of the tool.