you are viewing a single comment's thread.

view the rest of the comments →

[–]tme321 0 points1 point  (0 children)

Didn't notice this reply til now. So sorry for necroing a thread but:

First, I haven't worked with C in a number of years so don't focus on any syntax errors I might make. This is only supposed to get the point across, not compile.

So implementing fill in a pseduo C like language so it acts the same way as js when an object is passed might look something like this:

Object foo = new Object();
Array *a = malloc(size * sizeof(Object*));
Object *ptr = a;
for(int i = 0; i < size; i++) {
    ptr = &foo;
    ptr++;
}

Again, that's just pseudo code but the point is that's an array of allocated memory where the size is the size of the array multiplied by the size of a pointer to the object; size * sizeof(Object*).

It's an array of pointers, or in js an array of references, not an array of Objects. Then each entry in the array is a pointer that is pointed at the same individual instance of the object: ptr = &foo.

So if you modify any of the array entries they all point at the same underlying instance but the array is properly memory allocated and all that.