Hacker News new | comments | show | ask | jobs | submit login

> This doesn't make sense, a function is accessing a local variable of another function that has since been popped off the stack!

That's only weird to you because C doesn't do it.

For someone starting in one of those languages, it simply makes sense that I can use any variable in scope. (Stack allocation? What is this sorcery you speak of?)

So you're saying that my problem is having basic computer knowledge?

Because stack allocation has nothing to do with C, C is not explicit about doing it (except alloca) just like most other languages, and other languages use it just like C does, including C# with its closures and async/await. It even has stackalloc.

Your problem is knowing how most C implementations work and trying to extrapolate that knowledge to other languages.

In C, a variable's lifetime is limited to the current function call. The natural implementation of C is to track both function calls and variables on stack structure with statically determined offsets. Thus, C has stack-allocated variables.

Since a Clojure variable's lifetime is not limited to the current function call, C's implementation details aren't relevant. Stack-allocated variables are a bizarre notion.

For some reason (as I didn't mention Clojure which I know nothing about) you are making the incorrect assumption that my confusion with certain features comes from trying to apply how another language works to a different language.

In reality it comes from trying to apply how that language works in normal cases (in the absence of those features), and the features requiring special compiler magic to work.

Yes if variable lifetime is as you describe in Clojure then that sentence doesn't make sense for Clojure.

As I've already told someone else, it is unlikely that learning C at some point in your life (not as my first language, not as the language I use professionaly, not even in my top 3 most used languages) permanently cripples your ability to understand other languages.

Yes but stack allocation is completely transparent in all languages except C. Well, almost, maybe all low level languages, the point still stands.

In any non-systems language you don't need to know (and knowing it doesn't help you with anything) this stack sorcery you're talking about.

How is stack allocation less transparent in C than in other languages? I don't understand this idea that the stack has something to do with C.

You can mess with the stack in C, you can also mess with the stack in C#. But you don't have to, and normally you don't. You use it in a completely transparent manner, not caring where the compiler decides to put your data.

> A function that accesses a local variable of a function that has since been popped off the stack

Here is where it's less transparent. In a language like javascript there is no concept of a stack so what's in the quote doesn't make any sense.

In the C language there is no "concept of a stack" either because the stack is not a language-level feature in most languages, it is a mechanic relevant to how your code is compiled / executed. And javascript engines, when executing JS, do use the stack. You can see a stack trace for your code in Chrome...

When it comes to stack allocation, in C this is the compiler's decision and in JS this is the execution engine's decision.

In JS, that quote makes perfect sense, and the answer is: the engine checks what can be safely put on the stack, and the stuff that is closed over is stored on the heap to avoid losing it when the function returns.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact