969
you are viewing a single comment's thread
view the rest of the comments
[-] lime@feddit.nu 82 points 4 days ago

arrays don't have indices. lists have indices. arrays have offsets.

[-] The_Picard_Maneuver@lemmy.world 116 points 4 days ago

The real reason she won't call back.

[-] qprimed@lemmy.ml 23 points 4 days ago

indeed. permanently off by 1.

[-] turdas@suppo.fi 18 points 4 days ago

§6.7.9 of the C11 standard says they have elements with indices:

If an array of unknown size is initialized, its size is determined by the largest indexed element with an explicit initializer. The array type is completed at the end of its initializer list.

[-] lime@feddit.nu 7 points 4 days ago

it also states in section 6.7.7 ("type names") that

If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression.

note also that your example is the only occurrence of the word "index" in the entire document that isn't just referring to the actual index at the end.

[-] SorryQuick@lemmy.ca 4 points 3 days ago

Says who?

By definition, an index is

a number or symbol or expression (such as an exponent) associated with another to indicate a mathematical operation to be performed or to indicate use or position in an arrangement

Since the arrays offsets alao tell us about the items’s position in the array, is it not then an index?

People take these terms way too seriously. Hell, many languages have their “list” implemented as an array. What then do you call the index/offset?

[-] lime@feddit.nu 2 points 3 days ago* (last edited 3 days ago)

if you want my opinion (<- see now you can't tell me i'm wrong, it's an opinion) then the difference is that an array is by definition a memory address that's designated as the beginning of an array, and it's got an offset because the first element is at that specific address and further items are offset from that address. so you add the offset to the address to get the nth item. a list, meanwhile, can be basically any implementation under the hood, but it's commonly a linked list. the way you get the nth index there is you count up from the first position. since the implementation is opaque and may be spread out in memory you can't arithmetic your way to an index, you need to follow the pointers.

java's arraylist is a list backed by an array. java's vector is a list backed by a linked list.

[-] SorryQuick@lemmy.ca 3 points 3 days ago

That doesn’t really address what you call it. Names only really just exist to get your point across. Inexperienced devs may not know what an offset means (or why we use that), so index does the job. An experience dev knows how it works anyway, so whether you say index or offset won’t matter. By virtue of the common denominator, I simply use index everywhere.

[-] lime@feddit.nu 1 points 3 days ago

depends on the area you're working in. it's a pretty important distinction in embedded software.

[-] SorryQuick@lemmy.ca 3 points 3 days ago

Why? I’ve worked as an embedded dev for a few years and nobody in my team cared what it was called.

[-] lime@feddit.nu 1 points 3 days ago

because making sure offsets are correct come up a lot when you're memory-mapping IO.

[-] SorryQuick@lemmy.ca 2 points 3 days ago

Right but whether they’re correct or not doesn’t depend on the name you use. Every programmer worth his name knows arrays start at offset zero even if you don’t call it that.

[-] lime@feddit.nu 1 points 2 days ago* (last edited 2 days ago)

except if you use lua.

...which doesn't have arrays, but still.

[-] socsa@piefed.social 2 points 3 days ago

Um actually they have strides and offsets.

[-] vinnymac@lemmy.world 2 points 4 days ago
[-] lime@feddit.nu 4 points 4 days ago

javascript doesn't have arrays. the backing data structure is a doubly-linked list.

[-] qprimed@lemmy.ml 6 points 4 days ago

and the backing for that is linear or page addressed MOS transistors, spinning rust or flippy-round magnets.

do you have a source that indicates mainstream JS engines internally uses a list structure for arrays? I can't find one.

[-] lime@feddit.nu 4 points 4 days ago

skipped a few steps there i think.

anyway, good question. led me to some cursed code.

the ArrayObject in spidermonkey is an interface to either a TypedArrayObject or a SharedArrayObject. those both have an inner ArrayBuffer object, which is a view into ArrayBufferObjectMaybeShared, which contains a refcounted vector of uint8 pointers, regardless of the datatype. soooo all arrays in javascript are... strings?

[-] qprimed@lemmy.ml 3 points 3 days ago

skipped a few steps there i think.

thanks for the considered reply. didn't mean to jump all the way down to electrons and sound so flippant.

my claim is that JavaScript arrays are arrays because the spec defines their behavior as such. the implementation details are absolutely interesting from a performance perspective and I was genuinely curious how an internally linked list implementation would actually work, real-world. regardless... almost every interaction I have ever had with a JS programmer has ended in "its strings all the way down".. so... I mean... yes-ish?

loved your poking of the hornets nest in this thread :-)

[-] lime@feddit.nu 3 points 3 days ago

i was thinking between the linked list and the transistors :)

also, i mean... what you might call an array i might call a vector. js arrays allow elements of different types, so they are by definition not arrays in the traditional sense. them being chars internally does make sense in a gross way.

[-] thebestaquaman@lemmy.world 1 points 3 days ago

If I'm understanding you correctly, they're basically doing the same thing as Python under the hood and using a heap-allocated array (vector) of pointers? If so, that should still be orders of magnitude faster than a linked list.

If their implementation is actually a linked list, colour me shocked. My impression was that JavaScript is "decently fast". I've never even considered writing high-performance code in it, but I've heard that the compiler can optimise extremely aggressively, and it's used so widely that I couldn't imagine that it had glaring performance issues like what I would expect to see if every array was actually a linked list under the hood.

[-] lime@feddit.nu 1 points 3 days ago

can't a jit move things around enough that a linked list could be transformed into a memory-backed array if the access pattern requires it.

[-] thebestaquaman@lemmy.world 1 points 3 days ago

Sure it can, as long as it retains behaviour according to whatever standard it needs to comply to. My point was rather that I would be very surprised if the actual implementation (at memory level) was a linked list.

[-] FooBarrington@lemmy.world 2 points 4 days ago

That's a different kind of array (Float32Array etc.), not the "normal" kind.

[-] lime@feddit.nu 2 points 3 days ago

i couldn't actually find any of that in spidermonkey. i was looking in js/vm/arrayobject and its parents, didn't see any others.

[-] FooBarrington@lemmy.world 1 points 3 days ago
[-] lime@feddit.nu 1 points 3 days ago* (last edited 3 days ago)

i did look at that but i started out here: https://searchfox.org/firefox-main/source/js/src/vm/ArrayObject.h because it inherits from NativeObject.

[-] FooBarrington@lemmy.world 1 points 3 days ago

Yes, and since that's the wrong place I kept looking, and found the link I just sent you

[-] lime@feddit.nu 1 points 3 days ago

i edited my thing, i did start at array.cpp but only found references to other places so i went digging down the stack.

[-] vinnymac@lemmy.world 1 points 3 days ago

I’m aware, I added fuel, not peace and love

[-] Zink@programming.dev 1 points 3 days ago

You can't take my 'i', we have been together.

[-] lime@feddit.nu 1 points 3 days ago

you can't spell offset without off. as in fuck.

(affectionate)

this post was submitted on 12 May 2026
969 points (100.0% liked)

memes

21246 readers
2663 users here now

Community rules

1. Be civilNo trolling, bigotry or other insulting / annoying behaviour

2. No politicsThis is non-politics community. For political memes please go to !politicalmemes@lemmy.world

3. No recent repostsCheck for reposts when posting a meme, you can only repost after 1 month

4. No botsNo bots without the express approval of the mods or the admins

5. No Spam/Ads/AI SlopNo advertisements or spam. This is an instance rule and the only way to live. We also consider AI slop to be spam in this community and is subject to removal.

A collection of some classic Lemmy memes for your enjoyment

Sister communities

founded 2 years ago
MODERATORS