Re:C uses "&" for the address-operator because 'ampersand so...
by itsphilos on Tuesday October 10, 2023 @11:02AM
(#37832984)
I didn't know about Duff's device, which looks pretty interesting! Thanks for sharing
Re:C uses "&" for the address-operator because 'ampersand so...
by lisper on Tuesday October 10, 2023 @11:52AM
(#37833753)
Sure, but just for the record IMHO duffs device is an abomination and an illustration of how badly broken C is. No one should ever actually use it.
Re:C uses "&" for the address-operator because 'ampersand so...
by commandersaki on Wednesday October 11, 2023 @10:18AM
(#37844731)
I think I've come across Duff's device in Go internals for zero-ing memory.
Re:C uses "&" for the address-operator because 'ampersand so...
by codr7 on Tuesday October 10, 2023 @11:44AM
(#37833599)
Yeah, I remember having my mind blown when I first discovered Duff's Device.
My mental picture of what C-compilers are up to turned out to be a lot more sophisticated than reality.
But having written a few Forth-interpreters, it makes total sense to me. Treating the input as a mostly unstructured stream of tokens is very convenient.
Re:C uses "&" for the address-operator because 'ampersand so...
by bxparks on Tuesday October 10, 2023 @01:25PM
(#37834894)
I think computed-GOTO is easier to understand and more flexible than Duff's device, but computed-GOTO is not part of the C standard. It is sometimes available as a non-portable extension, e.g. https://gcc.gnu.org/onlinedocs/gcc/Labels-as-Values.html. Anyone know why it is not included in C?
Re:C uses "&" for the address-operator because 'ampersand so...
by lisper on Tuesday October 10, 2023 @01:28PM
(#37834933)
Computed go to was not quite an accurate description. I probably should have said “dispatch table” instead.
Re:C uses "&" for the address-operator because 'ampersand so...
by nuancebydefault on Tuesday October 10, 2023 @04:34PM
(#37837107)
What I find inconvenient is that a case body can be made with or without curly brackets. I'd rather have they were mandatory, like when an if statement aplies to more than one statement. The downside is that you need more indentation to have a logical curly brackets structure.
Choices/options for the same result, tend to make code less readable.
This was (almost certainly) done to simplify the compiler. CASE in C is not actually a structured control construct despite its syntactic appearance, it's just a computed GOTO. This is what makes things like Duff's device [1] possible.
[1] https://en.wikipedia.org/wiki/Duff%27s_device