←back to thread

Indices, not Pointers

(joegm.github.io)
102 points vitalnodo | 1 comments | | HN request time: 0.204s | source
Show context
zahlman ◴[] No.45110423[source]
> There is a pattern I’ve learned while using Zig which I’ve never seen used in any other language.

I've done this in small applications in C (where nodes were already being statically allocated) and/or assembly (hacking on an existing binary).

No idea about the effect on speed in general; I was trying to save a few bytes of storage in a place where that mattered.

replies(5): >>45111351 #>>45111431 #>>45111876 #>>45112740 #>>45112993 #
anonymousiam ◴[] No.45111431[source]
But in C, there's not really any difference between pointers and indices. You can access array elements using either method, and they're both equally efficient.
replies(4): >>45111456 #>>45111634 #>>45112113 #>>45112598 #
smadge ◴[] No.45112113[source]
The distinction in the article really is between calling malloc for every added node in your data structure (“pointer”) or using the pre-allocated memory in the next element of an array (“index”).
replies(1): >>45112652 #
1. adrian_b ◴[] No.45112652[source]
This is only one of the advantages discussed in TFA. The others are those due to using indices instead of pointers (like smaller size, cache locality, range checking, possibility of data exchange between systems with distinct address spaces).