6 ms·
> Reasoning about exceptional control flow w.r.t. manual memory management is a total nightmare. Exceptions make manual memory management easier because a prop
by sedachv 9y ago
> Reasoning about exceptional control flow w.r.t. manual memory management is a total nightmare.
Exceptions make manual memory management easier because a proper exception system has unwind-protect[1]. Exceptions are just movements up the stack - exceptions combine naturally with dynamic scoping for memory allocation (memory regions/pools). This kind of memory management was used in some Lisp systems in the 1980s, and made its way into C++ in the form of RAII. By extending the compiler you can add further memory management conveniences like smart pointers to this scheme.
Now if you want to talk about something that actually makes manual memory management a total nightmare, look at the OP's suggestion for adding closures to C.
[1] http://www.lispworks.com/documentation/HyperSpec/Body/s_unwind.htm http://www.lispworks.com/documentation/HyperSpec/Body/s_unwi...
- tomsmeding 9y agoC does not have RAII-like memory management in any way. Exceptions work beautifully with memory management like that, but if it's not there, you can't just say it should work because memory management should work like that. So basically you're saying, before adding exceptions, add RAII-like memory management, and then actually add exceptions. I like both features, but am not sure how you'd wedge RAII into C. Any ideas on that?
- BruceIV 9y ago[On the Cforall team] For what it's worth, one of the features Cforall adds to C is RAII. The exception implementation isn't done yet, but it's waiting on (limited) run-time type information, it already respects RAII.
- seabrookmx 9y agoI might have missed this.. but how is Cforall implemented? A new GCC or LLVM frontend, or is it a transpiles-to-C implementation ala. Nim or Vala?
- BruceIV 9y agoTranspiles-to-(GNU-)C -- it was first written before LLVM, if we were starting the project today it would likely be a Clang fork.
- seabrookmx 9y agoClang harks back to 2007 and LLVM 2003.. is this a research project that was recently taken back up? I was curious about the implementation because I've had rough experiences with Vala and Nim's approach. Unlike with "transpiles-to-js" languages, transpiling to C has some tooling gaps (debugging being the big one). I admittedly don't have a ton of experience with either language but I couldn't find a plugin that gave me a step-through debugger for something like CLion or VS Code. You can debug the C output directly but this will turn off newcomers and assumes the C output is clean.
- BruceIV 9y agoThe initial implementation was finished in '03, and we revived the project somewhere around '15, so your guess about a research project that was recently taken back up is correct. We intend to write a "proper compiler" at some point (probably either a Clang fork or a Cforall front-end on LLVM), but it hasn't been a priority for our limited engineering staff yet. I think we are getting a summer student to work on our debugging story (at least in GDB -- setting it up so it knows how to talk to our threading runtime and demangle our names), and improving our debugging capabilities has been a major focus of our pre-beta-release push.
- deleted 9y ago[deleted]
- RhysU 9y agoI have often wanted c99 with destructors for RAII purposes.
- baybal2 9y agoWhat I will like is more strict compile time checks. Most C pros have to rely on external tooling for that.
- BruceIV 9y agoIt's maybe not quite what you're looking for, but Cforall's polymorphic functions can eliminate nearly-all the unsafety of void-pointer-based polymorphism at little-to-no extra runtime cost (in fact, microbenchmarks in our as-yet-unpublished paper show speedup over void-pointer-based C in most cases due to more efficient generic type layout). As an example: forall(dtype T | sized(T)) T* malloc() { // in our stdlib return (T*)malloc(sizeof(T)); // calls libc malloc } int* i = malloc(); // infers T from return type
- baybal2 9y agoExcuse me for my lamerism, but can you tell me what is a polymorphic function? My idea was that if it is better to do as much compile time checks as possible before you introduce run-time checks. Does that void pointer protection run faster that code that was checked at compile time? How?
- BruceIV 9y agoA polymorphic function is one that can operate on different types[1]. You would maybe be familiar with them as template functions in C++, though where C++ compiles different versions of the template functions based on the parameters, we pass extra implicit parameters. The example above translates to something like the following in pure C: void* malloc_T(size_t sizeof_T, size_t alignof_T) { return malloc(sizeof_T); } int* i = (int*)malloc_T(sizeof(int), alignof(int)); In this case, since the compiler verifies that int is actually a type with known size (fulfilling `sized(T)`), it can generate all the casts and size parameters above, knowing they're correct. [1] To anyone inclined to bash my definition of polymorphism, I'm mostly talking about parametric polymorphism here, though Cforall also supports ad-hoc polymorphism (name-overloading). The phrasing I used accounts for both, and I simplified it for pedagogical reasons.
- sedachv 9y ago> C does not have RAII-like memory management in any way. C does not have memory management in any way period. The C standard library does. How you get to something with dynamic scoping like RAII in C is to use a different library for managing memory. For example Thinlisp[1] and Ravenbrook's Memory Pool System[2] both provide dynamically-scoped region/pool allocation schemes. [1] https://github.com/vsedach/Thinlisp-1.1 https://github.com/vsedach/Thinlisp-1.1 [2] https://www.ravenbrook.com/project/mps/ https://www.ravenbrook.com/project/mps/
- pjmlp 9y ago> C does not have RAII-like memory management in any way. Yes it does, as language extension on gcc and clang. It is called cleanup attribute. https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attribute... https://clang.llvm.org/docs/LanguageExtensions.html#non-standard-c-11-attributes https://clang.llvm.org/docs/LanguageExtensions.html#non-stan...
- tomsmeding 9y agoThanks! I learned something. Didn't know about that.
- EpicEng 9y agoThat's not C, _the language_, those are compiler features.
- pjmlp 9y agoI explicitly mentioned they are extensions.