logoalt Hacker News

frmdstryryesterday at 7:46 PM7 repliesview on HN

Do any languages have a notion of "relative pointers"? So in the example if instead of appending "line" as ptr & len, it'd instead be appending an offset & len which could in theory be used to safely compute the actual location even with relocations.


Replies

sparkietoday at 2:44 AM

I made a mini example in C.

It's awkward to do get right because you need an indirect pointer whose address remains fixed, but points to another pointer which can change (and is volatile).

While it might be possible to make something like this lockless - it's much simpler to stick a mutex in the array header. When we access the array_segment we can take a lock to prevent some other thread reallocating mid-way through accessing.

There's probably a few improvements that could be made. In particular it doesn't handle use-after-free, so it's not thread safe w.r.t cleanup.

https://godbolt.org/z/rYzn5KGre

amlutoyesterday at 9:17 PM

Languages with dependent types can express things like “this offset is in bounds relative to this other array”, which is maybe what you’re thinking of.

afdbcreidyesterday at 10:40 PM

That is called an index. If you want it to be standalone, you can bundle it with the ArrayList.

show 2 replies
surajrmaltoday at 12:24 AM

Not exactly what you asked, but c++ does this for vtables if you pass the right option to the compiler: -fexperimental-relative-c++-abi-vtables

There is a similar proposal for trait objects in rust.

Someoneyesterday at 8:19 PM

If you squeeze your eyes a bit, C compilers for Windows used to have them, with far pointers (https://en.wikipedia.org/wiki/Far_pointer)

Similarly, CPU architectures that use descriptors can (have to?) have languages with that notion.

show 2 replies
adzmyesterday at 11:36 PM

in c++, boost interprocess has offset_ptr which is useful since the shared data structure may be mapped at different locations in memory in each process

geonyesterday at 10:54 PM

Array indexing?