Hacker News (curated)new | past | comments | ask | show | jobs| show hidden

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.


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

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.

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.


The FS and GS segment selectors are still used in x86-64, typically for `thread_local` storage, but they can be repurposed.

`thread_local` is an example of a "relative pointer" though. Instructions to access the thread local are prefixed with `fs:` or `gs:`, and point relative to the address in the respective segment register.


After more searching I found this article https://www.gingerbill.org/article/2020/05/17/relative-point...

A far pointer sounds like the global based pointer described in that article. The far pointer Wikipedia article says they are problematic but doesn't give much reasoning as to why.


Far pointers are for accessing memory in different segments. They're basically obsolete now. They were necessary in older machines with limited sized pointers or address spaces.

GCC still supports `__seg_fs` and `__seg_gs`, which behave similar to `far` in the example on the wiki page, as the FS and GS segment registers are still valid in x86-64 and used for TLS. Clang uses attributes `address_space(257)` and `address_space(256)` for the same thing.

The `__based` pointer in MSVC exploits the addressing modes by pinning the base in eg: `[base+index*scale+displacement]`. It's unrelated to segmentation.


> Far pointers are for accessing memory in different segments. They're basically obsolete now.

Project CHERI would like to disagree.


Array indexing?



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact | github