0

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
Slev7n
  • 343
  • 3
  • 14

1 Answers1

1

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.

Rajendra arora
  • 2,186
  • 1
  • 16
  • 23
  • Thank you for your response. By pointer I meant an address in the memory that holds not a value but another address. – Slev7n Sep 26 '21 at 13:32
  • 1
    @Slev7n - you can say `references` as address. The stack contains a reference to the object in the heap. You can find more details in this [guidance link](https://felixgerschau.com/javascript-memory-management/). Let me know if it helps you? :) – Rajendra arora Sep 26 '21 at 13:51
  • It helps a lot thanks! So the variable object e.i the global scope lives in the stack section of the memory whereas all other objects including activation objects e.i. local scopes, in the heap? – Slev7n Sep 26 '21 at 16:10
  • 2
    @Slev7n You should consider that *everything* in JS is stored on the heap, minus those things that the engine can optimise to put on the stack (because they don't outlive the call) or in registers (because they are entirely temporary). – Bergi Sep 26 '21 at 19:01
  • 1
    @Rajendraarora "*Static data is data where the engine knows the size at compile time.*" - this is a weird definition that you quoted (?) from the article. Static means non-changing, and "static data" usually refers to constants etc. Not to *statically-sized data*. "*A stack is a data structure that JavaScript uses to store static data.*" - this is quite wrong. It's neither specific to JavaScript nor is *all* statically-sized data stored on the stack nor can *only* statically-sized data be stored on the stack. The article you link doesn't even describe the relevant core principle of a stack. – Bergi Sep 26 '21 at 19:06
  • @Bergi I've also read that V8 basically puts everything on the heap, except for smal integers. Interestignly most articles don't reflect this. Even those about the V8 engines. Do you know what the JS specification says about this? It's really hard to read. – tweekz Jan 03 '23 at 18:38
  • 1
    @tweekz The ECMAScript specifications says nothing about where to allocate memory. It doesn't even say a lot about garbage collection. – Bergi Jan 03 '23 at 19:33
  • @Bergi that's what I thought. So it's basically up to implementation. I found it confusing that some articles state that primitives are stored directly on the stack, whereas others suggest that even primitives are references that point to values. Considering this is up to implementation makes sense now. But I still have to get used to not having a "correct mental model" like in most other languages. – tweekz Jan 03 '23 at 19:42
  • @tweekz I don't know many languages that specify this, and no high-level languages (where you can't control memory layout) among them. Even in C, a compiler may choose to put variables on the stack or keep them in registers only. Most languages operate under an [as-if rule](https://en.wikipedia.org/wiki/As-if_rule), and optimising JIT compilation does whatever it wants. Point is: it doesn't matter. – Bergi Jan 04 '23 at 00:56
  • @Bergi So let me ask you how you think about it: If you pass a primitive to a function in JS, do you think of it as the value being copied from the actual to the formal parameter (with eventual in-place reassignment when changing the value of the formal parameter) or do you think of it as a reference being passed where the value is immutable and leads to allocation of new memory when reassigning? – tweekz Jan 04 '23 at 10:19
  • @tweekz Calling a function creates new variables (unless the function call is inlined and a variable is optimised away), passing a value as an argument causes it to be copied. Assigning a variable never causes new memory to be allocated, regardless whether a reference value or a primitive value is written into it. (It may lead to garbage collection if a reference value is overwritten). New memory might be allocated when creating a new value, not just for objects but also for some variable-size primitive JS data types like strings, bigints, symbols. – Bergi Jan 04 '23 at 14:53
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/250850/discussion-between-tweekz-and-bergi). – tweekz Jan 04 '23 at 18:36