Earlier quoted context omitted.
GCC's nested functions are implemented with trampolines, so there will be a performance penalty. For the amount that you gain, they seem like a bad idea. Tying your code to GCC for the sake of a small amount of convenience is a bad idea.
The question to which I responded did not inquire about performance nor about portability http://tirania.org/blog/archive/2011/Feb-17.html
Anonymous functions in C
41–50 of 51 posts
Re: Anonymous functions in C
#42Earlier quoted context omitted.
An objective-c object is just a pointer to a struct. You don't need the objective-c runtime to call a block. Block_copy etc are implemented in libclosure which iirc does not require libobjc either. But of course if you ARE writing objc, they are valid objects and can be treated as such
Correct, the problem is to call the function you cannot treat it as a standard function since it's pointing to a struct, the struct does contain the actual function pointer but there is an implicit `this' first argument to that function pointer which has to be that struct itself. This means you cannot use the block in an API that explicitly requires a function pointer, instead the API must specifically be aware of th…
Re: Anonymous functions in C
#43Earlier quoted context omitted.
You can take the address of a literal? Being able to specify a literal for a struct is useful. You can, for instance, put the literal in a macro and use the macro to initialize or reset a struct. It's better than having to write additional functions to do something trivial.
Yes, you can. You can use it to create an approximation of named parameters, because struct members that don't have an initializer will default to zero. It's still not nearly as compact as, say, Ruby, but for C it's pretty awesome and fast.
Re: Anonymous functions in C
#44Earlier quoted context omitted.
Nope, clang can be used on BSD, Linux, and Windows as well. http://clang.llvm.org/get_started.html
I'm aware of that, but do you get blocks?
Re: Anonymous functions in C
#45Earlier quoted context omitted.
An objective-c object is just a pointer to a struct. You don't need the objective-c runtime to call a block. Block_copy etc are implemented in libclosure which iirc does not require libobjc either. But of course if you ARE writing objc, they are valid objects and can be treated as such
Correct, the problem is to call the function you cannot treat it as a standard function since it's pointing to a struct, the struct does contain the actual function pointer but there is an implicit `this' first argument to that function pointer which has to be that struct itself. This means you cannot use the block in an API that explicitly requires a function pointer, instead the API must specifically be aware of th…
But if you write your program to use blocks from the start, that's not a big problem.
Re: Anonymous functions in C
#46Earlier quoted context omitted.
Yes, you can. You can use it to create an approximation of named parameters, because struct members that don't have an initializer will default to zero. It's still not nearly as compact as, say, Ruby, but for C it's pretty awesome and fast.
That's interesting. You know you can pass structs by value, too, so I'm not sure how necessary it is to take the address.
Re: Anonymous functions in C
#47Earlier quoted context omitted.
Some possible examples include qsort() and iterators. You could also use them for event handlers, but those are probably clearer as separate methods. I haven't used LambdaPP, but I'm guessing you can get around the lack of closures the same way you do with callbacks, by passing a void* with necessary data.
I'm sure that there are a lot of cases where you could make code look neater and sleeker with this kind of syntax extension, but my thesis -- and this is inherently hard to prove -- is that the uglier code you may have to write in C is nonetheless more robust and maintainable code in the end. A lot of very good software has been written in C, and what I suspect contributes to C's impressive record of robustness is it…
The key word there is "almost." C does have lots of undefined behaviors.
Re: Anonymous functions in C
#48Earlier quoted context omitted.
Indeed, I don't think you do: Blocks are supported for programs developed for Mac OS X 10.6+ and iOS 4.0+,[1] although third-party runtimes allow use on Mac OS X 10.5 and iOS 2.2+.[2] http://en.wikipedia.org/wiki/Blocks_(C_language_extension)
That page does mention Linux in one section: https://en.wikipedia.org/wiki/Blocks_%28C_language_extension...
Regardless, both of you appear to be correct:
BlocksRuntime - a target-independent implementation of
Apple "Blocks" runtime interfaces.
http://compiler-rt.llvm.org/Re: Anonymous functions in C
#49Wondering how a debugger would react to this code?
Lambdapp inserts #line directives into the source code, compilers are required to respect those directives and utilize them when producing debug sections in the binary. For instance in the case of gcc/clang on *nix the compiler will produce .debug_line sections as part of the DWARF debug format. Debuggers like gdb and even valgrind utilize this information to provide correct output. So to answer your question, a debu…
Similar problem is with say Qt's generated moc_Xxx source, Ui_xxx source, etc. files - unless you make the effort of storing these generated files somewhere you might have problems debugger later.
This is in general my "arrgh" against code generation, and "aargh" is not against it - it's simply when you had forgot to keep the files somewhere and the crashdump snaps fingers at you...
Re: Anonymous functions in C
#50Earlier quoted context omitted.
I'm sure that there are a lot of cases where you could make code look neater and sleeker with this kind of syntax extension, but my thesis -- and this is inherently hard to prove -- is that the uglier code you may have to write in C is nonetheless more robust and maintainable code in the end. A lot of very good software has been written in C, and what I suspect contributes to C's impressive record of robustness is it…
... almost no room for ambiguity and subtle, invisible side effects. What you see is what you get. [emphasis mine] The key word there is "almost." C does have lots of undefined behaviors.
The kind of ambiguity and side-effects I'm alluding to are ones programming languages explicitly support and encourage, like overloaded operators and overloaded methods in C++. In C, the effects of a segment of code are comparatively predictable just by looking at the code itself. In quite a large number of languages, you can't draw many conclusions about what a segment of code does without looking in multiple other places.