Live data from Hacker News

Anonymous functions in C

github.com

31–40 of 51 posts

Re: Anonymous functions in C

#31

Earlier quoted context omitted.

I'm aware of that, but do you get blocks?

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...

Re: Anonymous functions in C

#32

Earlier quoted context omitted.

The problem with clang blocks is they're represented as a Objective-C object, this makes them unusable in APIs that expect a function pointer, the only way you can cast them to a function pointer is to define the structure which represents the block and mmap executable code pages to marshal the call. Such a library exists that binds them to libffi here https://github.com/mikeash/MABlockClosure This feat alone makes b…

I don't think they could be an ObjectiveC object, because they're described as C, not ObjectiveC. I think the problems you're describing are ones that are going to be faced in any attempt at C closures. Closures have memory attached, that's the appeal of them and also the source of all the problems.

Nope, they're an ObjectiveC object with some specialties at the compiler level. Google how they're implemented.

Re: Anonymous functions in C

#34
post #17

Earlier quoted context omitted.

As long as the stack frame is still active, the local variables are still alive and valid. So you could pass a local function as a parameter, but you could not return it. I think jwz called these "downward funargs". GCC supports it http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html

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

Re: Anonymous functions in C

#35
post #33

Wondering 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 debugger would react to this code exactly how it should react, as if the lambda was called via a function and the file / line should be correct.

Re: Anonymous functions in C

#36

Earlier quoted context omitted.

I don't think they could be an ObjectiveC object, because they're described as C, not ObjectiveC. I think the problems you're describing are ones that are going to be faced in any attempt at C closures. Closures have memory attached, that's the appeal of them and also the source of all the problems.

Nope, they're an ObjectiveC object with some specialties at the compiler level. Google how they're implemented.

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

Re: Anonymous functions in C

#37

Earlier quoted context omitted.

I'm aware of that, but do you get blocks?

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)

You do, I've used them on Linux myself.

Re: Anonymous functions in C

#38

Earlier quoted context omitted.

Nope, they're an ObjectiveC object with some specialties at the compiler level. Google how they're implemented.

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 the block and would need to support it.

Re: Anonymous functions in C

#39
post #17

Earlier quoted context omitted.

As long as the stack frame is still active, the local variables are still alive and valid. So you could pass a local function as a parameter, but you could not return it. I think jwz called these "downward funargs". GCC supports it http://gcc.gnu.org/onlinedocs/gcc/Nested-Functions.html

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.

With things like PaX the trampoline method won't work, gcc now creates thunks, which are essentially heap allocated thunks of memory (hence the name) with PROT_EXEC.

Re: Anonymous functions in C

#40

Earlier quoted context omitted.

Callbacks are inherently going to make a program harder to follow. Without seeing the code, it's hard to suggest other solutions.

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 its spare syntax and the fact that there is almost no room for ambiguity and subtle, invisible side effects. What you see is what you get.

Adding simple, inline anonymous functions like these is a fairly modest proposal, of course, and I can't tell you exactly what damage they'd cause. They could turn out harmless. But they are redundant, because C already has a syntax for functions, and that redundancy now forces the programmer to have to make a choice about which syntax to use every time the situation permits them. Reading code becomes a bit more complicated, because there are more kinds of syntactic elements to recognize and know the consequences of. You end up splitting your time between two ways of writing functions, and that works against developing consistent practices and conventions for writing functions.

Is there a trade-off? Probably. It may be too onerous to write some types of code in C, and a little syntactic sugar might be able to make it tractable. But maybe you're better off writing that code in a different language, anyway. C has a good thing going, so why risk messing with it?

Post reply on HN