# Print value behind raw pointer without dropping

**URL:** <https://internals.rust-lang.org/t/print-value-behind-raw-pointer-without-dropping/16383>\
**Category:** Uncategorized\
**Created:** [March 28, 2022, 6:04pm UTC](https://internals.rust-lang.org/t/print-value-behind-raw-pointer-without-dropping/16383 "2022-03-28T18:04:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![superhawk610](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/superhawk610/32/9232_2.png) [@superhawk610](https://internals.rust-lang.org/u/superhawk610)\
**Post date:** [March 28, 2022, 6:04pm UTC](https://internals.rust-lang.org/t/print-value-behind-raw-pointer-without-dropping/16383/1 "2022-03-28T18:04:53Z")

</div>

I've been fiddling with some FFI with a Ruby project and I'm curious if this is the idiomatic way of printing/interacting with a value behind a raw pointer without dropping it.

```rust
#[no_mangle]
pub extern "C" fn sql_debug_query(query: *mut Query) {
    let query = unsafe { Box::from_raw(query) };
    println!("{:?}", query);
    std::mem::forget(query); // this is required so the Box destructor isn't called, right?
}

```

I have a method to create a query that uses `Box::into_raw(Box::new(query))`, and a complementary method to free a query that just does `unsafe { Box::from_raw(query); }`. My question - is this (`Box::from_raw / ... / std::mem::forget`) the correct/community-accepted method of inspecting a value behind a raw pointer? Is there any more concise way to express this, something like `query.box_borrow_raw` that won't drop the Box when it goes out of scope? Should my fn accept `*mut *mut Query` instead?

Thanks for any advice!

---

<div class="post-metadata">

**Author:** ![SkiFire13](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/skifire13/32/7579_2.png) [@SkiFire13](https://internals.rust-lang.org/u/SkiFire13)\
**Post date:** [March 28, 2022, 6:10pm UTC](https://internals.rust-lang.org/t/print-value-behind-raw-pointer-without-dropping/16383/2 "2022-03-28T18:10:52Z")

</div>

You can just reborrow it:

```rust
let query = unsafe { &*query };
println!("{:?}", query);

```

Edit: that function should be marked `unsafe` though

---

<div class="post-metadata">

**Author:** ![Jules-Bertholet](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/jules-bertholet/32/10671_2.png) [@Jules-Bertholet](https://internals.rust-lang.org/u/Jules-Bertholet)\
**Post date:** [March 28, 2022, 6:19pm UTC](https://internals.rust-lang.org/t/print-value-behind-raw-pointer-without-dropping/16383/3 "2022-03-28T18:19:55Z")

</div>

To add to @SkiFire13 's answer:

1. If your function can't free the memory that `query` points to, then it doesn't own that memory. So you want a reference, not an owned value like `Box`.
2. Your function leads to undefined behavior if a null, unaligned, or dangling pointer is passed in. Because null pointers can be constructed in safe code, you need to mark your function as `unsafe` and document that it has a safety invariant that the compiler can't check.

---

<div class="post-metadata">

**Author:** ![mjbshaw](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/mjbshaw/32/5103_2.png) [@mjbshaw](https://internals.rust-lang.org/u/mjbshaw)\
**Post date:** [March 28, 2022, 7:06pm UTC](https://internals.rust-lang.org/t/print-value-behind-raw-pointer-without-dropping/16383/4 "2022-03-28T19:06:17Z")

</div>

It seems like [users.rust-lang.org](http://users.rust-lang.org) is a better fit for this thread ([internals.rust-lang.org](http://internals.rust-lang.org) is for the development of Rust (the language) and associated tooling (e.g., the rustc compiler)).

---

<div class="post-metadata">

**Author:** ![superhawk610](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/superhawk610/32/9232_2.png) [@superhawk610](https://internals.rust-lang.org/u/superhawk610)\
**Post date:** [March 28, 2022, 10:51pm UTC](https://internals.rust-lang.org/t/print-value-behind-raw-pointer-without-dropping/16383/5 "2022-03-28T22:51:03Z")

</div>

Ah! I wondered why I couldn’t find any non-internal tags and why I had to register an account to post, didn’t even think to check the URL.

Thanks all for the help anyways, I’ll use the correct forum next time 🙃

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [June 26, 2022, 10:51pm UTC](https://internals.rust-lang.org/t/print-value-behind-raw-pointer-without-dropping/16383/6 "2022-06-26T22:51:30Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
