Re:URLs in C (2011)
by robalni on Wednesday November 04, 2020 @11:19AM
(#24990374)
This reminds me of when I tried to write HTML code in bash.
<html> hi </html> a
This code when run as a bash script reads from a file with name "html" and writes to file "hi".
It also reads from file "html" (in the root directory) and writes to a file called "a".
The "a" in the end is just to avoid bash errors about unexpected newline.
When I played with this a few years ago I actually discovered a bug in bash.
If I had a bash alias with the same name as one of the filenames, bash would do alias expansion on the filename.
This bug was triggered by executing a line with two redirection operators before any command.
So if you follow these steps:
$ alias a='abc xyz'
$ </ >a
you would get the error (in addition to a file created with name "abc"):
bash: xyz: command not found
This happened because bash expanded the second line to:
$ </ >abc xyz
and therefore treated "xyz" as a command (and "abc" and the root directory as filenames).
This bug doesn't work anymore in new versions of bash because it has been fixed.
Re:URLs in C (2011)
by nayuki on Wednesday November 04, 2020 @08:52AM
(#24989020)
The gotcha is that if in the same function you put in another URL with the same protocol, the compiler will refuse to compile the code due to a duplicate label.
int main() {
https://acceptable/
http://okay/
https://duplicate.error/
}
Re:URLs in C (2011)
by kevincox on Wednesday November 04, 2020 @09:53AM
(#24989547)
Somehow I quickly recognized the `https:` as a label. But I could not for the life of me figure out how `//` would compile. I was thinking it must be some sort of operator.
Brains are funny sometimes, and it is amazing how much we rely on context to remember and parse things.
Re:URLs in C (2011)
by uryga on Wednesday November 04, 2020 @02:18PM
(#24992110)
i think GP means that conceptually, you could imagine goto-ing to a function's code - they do compile to labels after all.
fun note: C-- has something like that! iirc it's like calling a function, but reusing the current stackframe:
jump f(expr1,. . .,exprn);
> "the `jump` statement performs a control jump but carrying parameters. It has as target [...] a procedure address and can optionally transfer arguments to that procedure."
Re:URLs in C (2011)
by uryga on Wednesday November 04, 2020 @07:05PM
(#24994388)
i always thought "tail call" means a function call that can be optimized in this way (located in a tail position), not the act of call-by-jumping itself. both uses make sense though
re: C--, i think it's mostly used as a compiler IR. (notably in Haskell's GHC)
And C/C++ also has the "-->" operator which, of course, is the "goes to zero" operator: https://stackoverflow.com/questions/1642028/what-is-the-oper...
And the famous joke thread from Stackoverflow with examples of file format abuse: https://stackoverflow.com/questions/5508110/why-is-this-prog...
When I played with this a few years ago I actually discovered a bug in bash. If I had a bash alias with the same name as one of the filenames, bash would do alias expansion on the filename. This bug was triggered by executing a line with two redirection operators before any command.
So if you follow these steps:
you would get the error (in addition to a file created with name "abc"): This happened because bash expanded the second line to: and therefore treated "xyz" as a command (and "abc" and the root directory as filenames). This bug doesn't work anymore in new versions of bash because it has been fixed.gcc for example gives the error "error: a label can only be part of a statement and a declaration is not a statement" for the code in OP's comment.
g++ happily compiles it.
See the first answer here: [1]
[0] https://en.wikipedia.org/wiki/Statement_(computer_science)
[1] https://stackoverflow.com/questions/49861965/declarations-de...
Brains are funny sometimes, and it is amazing how much we rely on context to remember and parse things.
Full working demo: https://gist.github.com/jasom/41ab3712d83d7edbdc03bb8e43a7af...
Where to goto? A label, of course.
I wrote this:
It creates error: How can we goto a function name?fun note: C-- has something like that! iirc it's like calling a function, but reusing the current stackframe:
> "the `jump` statement performs a control jump but carrying parameters. It has as target [...] a procedure address and can optionally transfer arguments to that procedure."https://www.microsoft.com/en-us/research/wp-content/uploads/...
The concept is not so unique as you might think tho; the more commonly used term is "tail call".
re: C--, i think it's mostly used as a compiler IR. (notably in Haskell's GHC)
Nothing special
// nothing to see here