简体   繁体   中英

Javascript memory allocation: heap and stack

Does the heap only contain pointers? And the stack is the place where the actual data resides?

Pointers on the heap section point to, either other pointers in the heap, or they point to the values stored in the stack section of the memory.

Is it true?

From this documentation:

Stack:

A stack is a data structure that JavaScript uses to store static data. Static data is data where the engine knows the size at compile time. In JavaScript, this includes primitive values (strings, numbers, booleans, undefined, and null) and references, which point to objects and functions.

Heap:

The heap is a different space for storing data where JavaScript stores objects and functions.

Coming to your question: Does the heap only contain pointers?

No, JS doesn't have pointers. You can consider objects are pointers in JS.

Unlike in C, you can't see the actual address of the pointer nor the actual value of the pointer, you can only dereference it (get the value at the address it points to.)

From this , here is a nice example with explanation:

//this will make object1 point to the memory location that object2 is pointing at
object1 = object2;

//this will make object2 point to the memory location that object1 is pointing at 
function myfunc(object2){}
myfunc(object1);

Let me know if it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM