7 ms·
The "hackaday" blog focuses on cool things that are typically (but not necessarily) impractical. He isn't suggesting that this "lambda" be used. Instead, this i
by dragontamer 7y ago
The "hackaday" blog focuses on cool things that are typically (but not necessarily) impractical. He isn't suggesting that this "lambda" be used. Instead, this is a stealth blog-post about "__anon", as far as I can tell.
Which is really what hackaday is about: finding weird features in hardware/compilers/etc. etc. and using them in some manner. There's a whole lot of obscure features of GCC that are being touched upon in this blogpost (nested functions, whatever is going on with $__anon$, etc. etc.). I can't say that I can figure out exactly what is going on yet, but its kind of exciting to see all of these features get used at once.
https://github.com/wd5gnr/clambda/blob/master/clambda2.c https://github.com/wd5gnr/clambda/blob/master/clambda2.c
EDIT: Unfortunately, it just segfaults for me at the moment.
$ gcc --std=gnu99 clambda2.c
$ ./a.out
Segmentation fault (core dumped)
$ gcc --version
gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
This is Ubuntu on Windows, but I doubt that would make a difference.
- deleted 7y ago[deleted]
- johnisgood 7y agoIt works for me. I am using Linux. $ gcc clambda2.c && ./a.out Running sum=6.282000 Running sum=18.322001 Running sum=102.321999 Running sum=103.722000 25.930500 Running sum=1.047000 Running sum=3.053667 Running sum=17.053667 Running sum=17.287001 4.321750 $ gcc --version gcc (GCC) 9.1.0 [...]
- archgoon 7y agoIt does make a difference for some unknown reason. :) > However, using the Linux system for Windows 10, the same code would seg fault. In fact, if you didn’t set the gcc -O2 option, the other examples would seg fault, too.
- dragontamer 7y agoStrangely enough, it works with -O2. There's clearly some kind of undefined-behavior going on (that depends on the optimizer!), since the code doesn't work with -O0.
- quietbritishjim 7y ago> whatever is going on with $__anon The "lambda$__anon$" identifer is just the name of the local function, it could just as well have been "elephant" or anything else. The first line defines the nested function: { double elephant (double x){ return x/3; } And the second line references that same identifer: &elephant; } Normally an expression that didn't include an assignment call or a function call is legal but doesn't do anything. But as the article mentions, GCC uses it as the return value of the block. The commenters seem to have identified the undefined behaviour here: the resulting value is a pointer to a function that's only valid within the block but is being used outside it.
- ramshorns 7y agoWhat do the dollar signs do? If they're really just part of the identifier it doesn't seem necessary to make sure the compiler supports it, rather than just use a more normal name like elephant.
- gpderetta 7y agoIt is just a way to uglify symbols to make collisions with surrounding code less likely. C macros are not hygienic.
- deleted 7y ago[deleted]