Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are variables in a closure stored - stack or heap?

Tags:

Like the following codes:

var foo = function() {
    var a = 1; // closure var
    return function() { // closure fun
        console.log(a);
    }
};
var bar = foo();

When foo exits(or say, returns), we know that the variable a will not be destroyed and remains in memory(that's why closure works). So my problem is where does the the variable a store, stack or heap?

like image 340
JunGor Avatar asked Mar 24 '15 05:03

JunGor


People also ask

Are variables stored in stack or heap?

Stack and a Heap ? Stack is used for static memory allocation and Heap for dynamic memory allocation, both stored in the computer's RAM . Variables allocated on the stack are stored directly to the memory and access to this memory is very fast, and it's allocation is dealt with when the program is compiled.

Are variables stored on the heap?

The heap is a memory used by programming languages to store global variables. By default, all global variable are stored in heap memory space. It supports Dynamic memory allocation. The heap is not managed automatically for you and is not as tightly managed by the CPU.

Are variables stored on the stack?

The stack is used for dynamic memory allocation, and local variables are stored at the top of the stack in a stack frame. A frame pointer is used to refer to local variables in the stack frame.

Where are stack variables stored?

They can point anywhere. Not only on the stack or data segment. Think of an pointer which points to an array of pointers. In this case the pointers in the array are stored on the heap.


1 Answers

A closure is just an evolution of the concept of the stack.

The stack is used to separate/isolate scope when functions are called. When a function returns the stack frame (activation record) is popped off the call stack thus freeing the used memory allowing the next function call to reuse that RAM for its stack frame.

What a closure does is that instead of actually freeing that stack frame, if there's any object/variable in that stack frame that's referenced by anything else then it keeps that stack frame for future use.

Most languages implement this by implementing the stack as a linked list or hash table instead of a flat array. That way, the stack can be re-ordered at runtime and is not constrained by physical memory layout.

So. With this in mind, the answer is that variables in a closure are stored in the stack and heap. Depending on your point of view.

From the point of view of the language, it's definitely the stack. Since that's what closures are in theory - a modified stack.

From the point of view of the machine language or underlying C/assembly code, the idea of a linked-list stack is nonsense. Therefore the higher level language must be using the heap to implement its "stack".

So the variable is in the stack but that stack is probably located in the heap.

This of course depends on the implementation of your programming language. But the above description is valid for most javascript interpreters (certainly all the ones I've seen).

like image 137
slebetman Avatar answered Sep 19 '22 15:09

slebetman