0

Can someone translate this into something that makes sense for me:

Converts the pointer to a raw pointer (without the tag).

  1. What is the difference between a pointer and a raw pointer?

    The Stack Overflow raw-pointer tag says neither "smart" nor "shared" which again is mystifying.

  2. What are Crossbeam's Shared::as_raw's "tags" all about?

JGFMK
  • 8,425
  • 4
  • 58
  • 92
  • https://doc.rust-lang.org/book/ch19-01-unsafe-rust.html - I seem to have found something about it here... – JGFMK Aug 23 '20 at 17:10
  • 2
    I just typed "rust raw pointer" into DuckDuckGo and I got [this](https://doc.rust-lang.org/std/primitive.pointer.html) (first hit). It's not unreasonable to expect askers to put in at least that much effort, don't you agree? – trent Aug 23 '20 at 17:22
  • 1
    @trentcl I didn't know it was specific to Rust. Most pointer stuff I've come across before was with C many years ago. So trying it without Rust wasn't so helpful..I went in search of answers on Wiki and found nothing before posting. Furthermore, the link you provided might as well have been written in Greek for all the sense it made to me. My post included two parts as well, not just 'raw pointer' The answer below helped far more. – JGFMK Aug 23 '20 at 18:07
  • 1
    Raw pointers are not, in fact, specific to Rust, but even if they were, you could still have looked up some documentation, tried to understand it, and posted a question containing a line like "I found this page, but I don't understand it *because*..." Failing to do research before asking a question, or failing to *include* that research in the question, is likely to lead to downvotes. [ask] – trent Aug 23 '20 at 18:11
  • @JGFMK I imagine one or two of those downvotes would become upvotes if you allowed the question to be improved... – Peter Hall Aug 26 '20 at 19:09
  • 1
    [When is “EDIT”/“UPDATE” appropriate in a post?](https://meta.stackexchange.com/q/127639/281829) – Shepmaster Aug 26 '20 at 19:14
  • 1
    *it looks like I have asked and answered one half of the question myself* — you **have**. Instead, you should edit the answer, comment on the answer, or add your own answer. – Shepmaster Aug 26 '20 at 19:15
  • @trentcl It was unfortunate I Googled on "wiki raw pointer". So I did not blindly post without searching as you inferred. It also seems from subsequent dialogs in the Stack Overflow comments with mcarton that a lot of the C++ ones were misleading too in the context of this crate. So the effort to wrap my head around this was far more painful than the results a search engine provided offered. – JGFMK Aug 26 '20 at 20:14
  • i.e. I thought this Microsoft C++ article https://docs.microsoft.com/en-us/cpp/cpp/raw-pointers?view=vs-2019 explained raw pointers in layman's terms better than the current Rust documentation. @mcarton response: It defines “A raw pointer is a pointer whose lifetime is not controlled by an encapsulating object, such as a smart pointer.” Except that in the case of `crossbeam_epoch::Shared` the raw pointer has more to do with not having the tag than any lifetime. For someone from a non-Systems programming background, used to garbage collection languages it's easy to miss such subtleties. – JGFMK Aug 27 '20 at 06:10

1 Answers1

4
  • crossbeam_epoch::Shared is a smart pointer. That is, a pointer plus extra stuff. In C++ or Rust, smart pointer is the term used for a pointer wrapper which adds any of the following:

    • Ownership information
    • Lifetime information
    • Packing extra data in unused bits
    • Copy-on-write behavior
    • Reference counting

    In that context, a raw pointer is just the wrapped pointer, without all the extra stuff.

  • crossbeam_epoch::Shared fits (among others) in the “Packing extra data in unused bits” category above. Most data in modern computers is aligned, that is, addresses are a multiple of some power of two. This means that all low bits of the addresses are always 0. One can use that fact to store a few extra bits of information in a pointer.

    This extra data is called tag by this particular library, however that term isn't as common as raw pointer.

mcarton
  • 27,633
  • 5
  • 85
  • 95
  • https://en.wikipedia.org/wiki/Copy-on-write - in case anyone else was mystified by that one. – JGFMK Aug 23 '20 at 17:30
  • 2
    Pointer tagging is not Crossbeam-specific, see [wikipedia](https://en.wikipedia.org/wiki/Tagged_pointer). I expect crossbeam finds it useful for preventing the [ABA problem](https://en.wikipedia.org/wiki/ABA_problem), where two pointers in a lock-free system have the same address, but are still distinct because they come from different times. When a block of memory is freed, a subsequent allocation can give the same address and replacing the pointer atomically can falsely indicate that no change has taken place. Tags can discriminate pointers to the same location from different generations. – user4815162342 Aug 24 '20 at 08:29