7 ms·
I'm all for the evolution of C, but this list... 1) has some downright idiotic things (exceptions, operator overloading) 2) has a few reasonable, but mostly i
by enriquto 9y ago
I'm all for the evolution of C, but this list...
1) has some downright idiotic things (exceptions, operator overloading)
2) has a few reasonable, but mostly inconsequential things (declaration inside if, case ranges)
3) is missing a few real improvements (closures, although it is not clear whether the "nested routines" can be returned)
- bdamm 9y agoAgree 100%. Improvements to C would be things like removing "undefined behavior", not adding more syntax sugar. If anything, C's grammar is already too bloated. (I'm looking at you, function pointer type declarations inside anonymized unions inside parameter definition lists.)
- bhasi 9y ago> function pointer type declarations inside anonymized unions inside parameter definition lists Could you please provide a code snippet of this kind? Hard for me to visualize otherwise. Thanks.
- pjmlp 9y agoSure, lets do it in pieces typedef int (*my_func_ptr_t)(const void*,const void*); typedef union { my_func_ptr_t func_ptr; int* data; } my_union_t; void my_func (my_union_t param); // Now exploiding it void my_func (union { int (*func_ptr)(const void*,const void*); int* data; } param); Of course this is very basic example, but there is hope to make it into an IOCC entry.
- geezerjay 9y ago> Improvements to C would be things like removing "undefined behavior" This nonsense again. I don't get this "undefined behavior" cliche. It seems it became fashionable for some people to parrot it like a mantra as a form of signaling. Undefined behavior just refers to something that is not covered by the international standard, and therefore doesn't exist nor should be used, but an implementation may offer implementation-specific behavior.
- blackguardx 9y agoI think many people overlook that "undefined behavior" mainly limits portability. Invoking undefined behavior won't cause your program to do random things, it just might not do the same thing when you switch to a different compiler. This is still a big problem, but not as big as people make it out to be, in my opinion. I think undefined behavior problems in the standard could be fixed, but compilers would need special flags for backwards compatibility with programs that rely on it.
- aw1621107 9y ago> "undefined behavior" mainly limits portability I had the impression that some things are UB because they can't specify a single behavior that would be efficient across all platforms C targets.
- fjsolwmv 9y agoNo that's implementation-defined. Undefined means code that is logically incorrect, but too expensive for the compiler/runtime to check and handle (reject) it. A simple example is array out of bounds access. It's too expensive to demand the compiler prevent it or guarantee to trigger some kind of signal at runtime. But if you are willing to pay the cost , you can use a language or compiler that is safer and promises to throw an exception or provides safe statically sizd arrays
- UncleMeat 9y ago> Invoking undefined behavior won't cause your program to do random things According to the spec, it literally can. The compiler is free to replace your entire program with unrelated functionality. It can ever do a different thing each time it compiles the program. There are implementation specific behaviors (# of bits in a char), which are different.
- deleted 9y ago[deleted]
- 9y ago
- pmarreck 9y agoCan you explain why exceptions and operator overloading are "idiotic" things? Are you from the Go school of boilerplate-error-checking-code design, or something?
- adrift 9y agoSum types are the new trend.
- dmitrygr 9y agoexceptions because in embedded contexts they may not always be a good idea (and C targets such contexts). overloading because it is too easy to abuse and as such it gets abused a lot by those who do not know better. The rest of us are then stuck decoding what the hell "operator +" means when applied to a "serial port driver" object
- UncleMeat 9y agoExceptions work great in garbage collected languages. Reasoning about exceptional control flow w.r.t. manual memory management is a total nightmare.
- 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 ago
- dmitrygr 9y agoCouldn't agree more. and i'll add another: the suggested syntax is ridiculous. What is this punctuation soup? void ?{}( S & s, int asize ) with( s ) { // constructor operator void ^?{}( S & s ) with( s ) { // destructor operator ^x{}; ^y{}; // explicit calls to de-initialize
- reza_n 9y agoConstructs like closures come at a cost. Function call abstraction and locality means hardware cannot easily prefetch, instruction cache misses, data cache misses, memory copying, basically, a lot of the slowness you see in dynamic languages. The point of C is to map as close to hardware as possible, so unless these constructs are free, better off without them and sticking to what CPUs can actually run at full speed.
- zzzcpan 9y agoClosures are logical abstractions and cost nothing, since they are logical. Naive runtime implementations of closures can of course be a bit slower than native functions, but so can be everything.
- alerighi 9y agoClousure costs a lot if we are talking of real closures, that capture variables from the scope where they are defined, because you need to save somewhere that information, so you need to alloc an object with all the complexity associated. And it can easily get very trick in a language like C where you don't have garbage collection and you have manually memory management, it's easy to capture things in a closure and then deallocate them, imagine if a closure captures a struct or an array that is allocated on the stack of a function for example. I think we don't need closures in C, the only thing that I think we would need is a form of syntactic for anonymous function, that cannot capture anything of course, it will do most of the things that people uses closure for and doesn't have any performance problems or add complexity to the runtime.
- zzzcpan 9y agoIf a closure cannot be optimized out, i.e. the scope of the closure outlives the scope of the function it captures a variable from, than this closure is equivalent to a heap allocated struct, which cannot be allocated on the stack either if it outlives its scope. So the cost is still the same.
- steveklabnik 9y ago
- lloeki 9y ago> 3) is missing a few real improvements (closures, although it is not clear whether the "nested routines" can be returned) Ah, I wish Blocks[0] would have made to into the C language as a standard†... Although you can use them with clang already: $ clang -fblocks blocks-test.c # Mac OS X $ clang -fblocks blocks-test.c -lBlocksRuntime # Linux Since closures are poor man's object, I had some fun with them to fake object-orientedness[1]. † or at least that the copyright dispute between Apple and the FSF for integration into GCC would have been resolved (copyright transferred to the FSF being required in spite of a compatible license). [0]: https://en.wikipedia.org/wiki/Blocks_%28C_language_extension%29 https://en.wikipedia.org/wiki/Blocks_%28C_language_extension... [1]: https://github.com/lloeki/cblocks-clobj/blob/master/main.c#L13 https://github.com/lloeki/cblocks-clobj/blob/master/main.c#L...