| ▲ | unwind 8 hours ago | ||||||||||||||||||||||||||||||||||||||||
This was very interesting, and it's obvious from the majority of the text that the author knows a lot about these languages, their implementation, benchmarking corners, and so on. Really! Therefore it's very jarring with this text after the first C code example: This uses a static variable to have it persist between both the compare function calls that qsort makes and the main call which (potentially) changes its value to be 1 instead of 0 This feels completely made up, and/or some confusion about things that I would expect an author of a piece like this to really know. In reality, in this usage (at the global outermost scope level) `static` has nothing to do with persistence. All it does is make the variable "private" to the translation unit (C parliance, read as "C source code file"). The value will "persist" since the global outermost scope can't go out of scope while the program is running. It's different when used inside a function, then it makes the value persist between invocations, in practice typically by moving the variable from the stack to the "global data" which is generally heap-allocated as the program loads. Note that C does not mention the existence of a stack for local variables, but of course that is the typical implementation on modern systems. | |||||||||||||||||||||||||||||||||||||||||
| ▲ | CerryuDu 2 hours ago | parent | next [-] | ||||||||||||||||||||||||||||||||||||||||
I'm finding myself in a weird position now, because I disagree with a whole lot of things in the blog post (well, the parts I was willing to read anyways), but calling that variable static for the sake of persistence was correct. The fact that you are questioning the use of the term shows that you are not familiar with the ISO C standard. What the author alludes to is static storage duration. And whether or not you use the "static" keyword in that declaration (also definition), the storage duration of the object remains "static". People mostly call those things "global variables", but the proper standardese is "static storage duration". In that sense, the author was right to use "static" for the lifetime of the object. EDIT: if you drop "static" from that declaration, what changes is the linkage of the identifier (from internal to external). | |||||||||||||||||||||||||||||||||||||||||
| ▲ | gldrk 4 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
>This uses a static variable to have it persist between both the compare function calls that qsort makes and the main call which (potentially) changes its value to be 1 instead of 0 The only misleading thing here is that ‘static’ is monospaced in the article (this can’t be seen on HN). Other than that, ‘static variable’ can plausibly refer to an object with a static storage duration, which is what the C standard would call it. >moving the variable from the stack to the "global data" which is generally heap-allocated as the program loads It is not heap-allocated because you can’t free() it. Non-zero static data is not even anonymously mapped, it is file-backed with copy-on-write. | |||||||||||||||||||||||||||||||||||||||||
| ▲ | sfpotter 4 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
I had a completely different response reading the sentence. I've been programming in C for 20+ years and am very familiar with exactly the problem the author is discussing. When they referred to a "static variable", I understood immediately that they meant a file static variable private to the translation unit. Didn't feel contrived or made up to me at all; just a reflection of the author's expertise. Precision of language. | |||||||||||||||||||||||||||||||||||||||||
| ▲ | pjmlp 5 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
The author contributes to ISO C and ISO C++ working groups, and his latest contribution was #embed. | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||
| ▲ | debugnik 3 hours ago | parent | prev | next [-] | ||||||||||||||||||||||||||||||||||||||||
It took me a second read to realise that the mention of static is a red herring. I think the author knows that the linkage is irrelevant for the rest of the explanation; it just happens to be static so they called it static. But by drawing attention to it, it does first read like they're confused about the role of static there. | |||||||||||||||||||||||||||||||||||||||||
| ▲ | kreco 8 hours ago | parent | prev [-] | ||||||||||||||||||||||||||||||||||||||||
That's a very weird comment, your spreading your knowledge and not really addresse what could have been changed in the article. If I follow your comment, you mean that he could have use a non-static global variable instead and avoid mentioning "static" keyword afterward? | |||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||