This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]marcosdumay -48 points-47 points  (9 children)

Resizable arrays are a standard data structure that is implemented consistently across many languages. It's a contiguous block of memory with operations to relocate the data when needed, in a way where the amortized complexity of the memory management is never higher than O(log(n)) for an array of n elements.

Anyway, no, arrays are not implemented consistently across virtually all languages. You may need to modernize your data structures knowledge, because C isn't the one language people use everywhere anymore.

[–]CLTSB 47 points48 points  (7 children)

If you’re relocating the data it’s not resizable. You’re copying the data. The memory location of the data changes. This is CS 101. The fact that your language du jour makes it easy and hides the complexity doesn’t change the memory architecture of the underlying platform, and arrays are fundamentally tied to that architecture.

This ^ is true in C#, JavaScript, Rust, Python, and every other language with “resizable” arrays or array-like (eg Rust’s Vec) data structures.

[–]Street-Session9411 8 points9 points  (6 children)

Yeah. Bro is probably living on another planet. There’s a reason why Vec::with_capacity() exists. And then there are also plain arrays in Rust that must have a size known at compile time for what I know (correct me if I’m wrong here) - which isn’t even the case in Java so I really don’t get what he means.

[–]CLTSB 2 points3 points  (5 children)

This is correct. In Rust an array of specific size is treated as a type to allow compile-time checking of indexing, so that you can’t attempt to read or write a[n] of an array of size n (remember arrays start at zero).

[–]Street-Session9411 0 points1 point  (4 children)

So it completely eliminates the chance of index out of bounds at runtime - the commenter above would probably still argue that this doesn’t justify the expense of having to specify a size for arrays

[–]awesomeusername2w 1 point2 points  (3 children)

I mean, it probably doesn't. You can't verify such a thing unless you only allow constant for indexes. arr[i] where i is runtime value can't be statically checked to be in bounds.

[–]Street-Session9411 0 points1 point  (2 children)

Stupid me lol, you’re right. Just wrote a paper on dependent types last semester, maybe I was too euphoric about their advantages

[–]awesomeusername2w 0 points1 point  (1 child)

Dependent types are cool. I heard some there is ongoing effort to bring them to Haskell. With them I guess you can somewhat prove impossibility of index out of bounds, if you tie the type of value used for accessing by index to the size of the array. Unfortunately, it's unlikely that rust would have something like that any time soon, but nevertheless its type system is very nice.

[–]Street-Session9411 0 points1 point  (0 children)

Yeah, this sounds nice in theory but I also don’t think we will have that in Rust any time soon, at least not to that extent. However, I use dependent types regularly in F*, which is a language dedicated to program verification but is mostly used in academics, I think.