# Is there a way to check how "far away" a pointer is from a page boundary?

**URL:** https://discourse.julialang.org/t/is-there-a-way-to-check-how-far-away-a-pointer-is-from-a-page-boundary/8147
**Category:** General Usage
**Created:** [January 4, 2018, 4:34am UTC](https://discourse.julialang.org/t/is-there-a-way-to-check-how-far-away-a-pointer-is-from-a-page-boundary/8147 "2018-01-04T04:34:35Z")
**Posts on this page:** 1
**Showing post:** 11

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [January 5, 2018, 6:11pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-check-how-far-away-a-pointer-is-from-a-page-boundary/8147/11 "2018-01-05T18:11:17Z")

</div>

> [@xiaodai](#):
>
> The larger it is the closer it is to the edge?

Yes. Memory addresses are [direct-mapped to virtual-memory pages](https://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Memory/virtual.html), i.e. by looking at the memory address and masking out the least-significant bits. The least-significant bits are the offset with the page, and tell you how close you are to the page boundary. Since memory can only be marked unreadable by the VM system at the page level, in my understanding it is always safe (can’t segfault) to read memory within the same page as your data (if you are okay with reading garbage).

Assuming that the pages are a power-of-two size that is at least 4096 (4k, `0x1000` in hex) bytes, which I think is true on all x86\_64 systems at least, you can therefore check whether the data of a string `s` is less than 8 bytes from a page boundary by `(UInt(pointer(s)) & 0xfff) > 0xff8`. I’m not sure about the page sizes on x86, ARM, etc.

This is very finicky low-level stuff, though, with problematic portability. I would be _really_ sure that you have exhausted all other methods of optimization, and that this kind performance hack is really necessary, before considering it.

---

_[View the full topic](https://discourse.julialang.org/t/is-there-a-way-to-check-how-far-away-a-pointer-is-from-a-page-boundary/8147)._
