Questions tagged [raw-pointer]

Memory pointer that is neither a smart pointer nor shared pointer.

For more information check out the Microsoft C++ article on the topic

60 questions
36
votes
2 answers

What's the difference between raw pointer and weak_ptr?

As in title. This question probably already has an answer but I failed to find one.
NPS
  • 5,261
  • 8
  • 46
  • 80
16
votes
2 answers

Pointing to the content of std::unique_ptr

I have a std::unique_ptr and another raw pointer. I want the raw pointer to point to the content of the unique_ptr without any kind of ownership. It is read-only relationship: auto bar=std::make_unique(); auto ptr=bar.get();// This may point to…
Humam Helfawi
  • 17,706
  • 12
  • 64
  • 134
7
votes
1 answer

How should I decide when it is more or less appropriate to use raw pointers?

I get the impression that Rust is intended to be used in highly safe systems. Then I noticed that raw pointers allow arbitrary pointer arithmetic, and they can cause memory safety and security issues.
PHA
  • 1,372
  • 3
  • 13
  • 32
5
votes
1 answer

Why does `*mut T` implement `Unwindsafe` but `&mut T` doesn't?

In the documentation for Unwindsafe we have: Types such as &mut T and &RefCell are examples which are not unwind safe. The general idea is that any mutable state which can be shared across catch_unwind is not unwind safe by default. This is…
little-dude
  • 1,336
  • 2
  • 15
  • 28
5
votes
1 answer

Is it safe to clone a type-erased Arc via raw pointer?

I'm in a situation where I'm working with data wrapped in an Arc, and I sometimes end up using into_raw to get the raw pointer to the underlying data. My use case also calls for type-erasure, so the raw pointer often gets cast to a *const c_void,…
randomPoison
  • 1,100
  • 7
  • 13
4
votes
1 answer

Is the adress behind this guaruanteed to be identical to a variable with the object

In case of the following code: #include class Sample { public: Sample* getSelf() { return this; } }; int main() { Sample s; if(reinterpret_cast(&s) == reinterpret_cast(s.getSelf())) std::cout << "Same…
Jimmy R.T.
  • 1,274
  • 9
  • 13
4
votes
2 answers

Getting into smart pointers, how to deal with representing ownership?

i've made a dynamic graph structure where both nodes and arcs are classes (i mean arcs are an actual instance in memory, they are not implied by an adjacency list of nodes to nodes). Each node has a list of pointers to the arcs it's connected…
Barnack
  • 812
  • 5
  • 18
4
votes
1 answer

How to make a struct field containing an Arc writable?

I have a struct that somehow must be retrieved in the form of raw pointer. pub struct BufferData { /// Memory map for pixel data pub map: Arc>, pub otherdata: i32, } I need to write into its map field, so I…
Abdillah
  • 788
  • 9
  • 24
3
votes
1 answer

Unexpected pixels changing in opengl texture before being drawn to screen

I am writing a framework to be able to draw pixels to the screen. However now that I am trying to update the screen the first 4 pixels are showing random colors. I did not have this problem when I was just sending a pointer to the image data to my…
3
votes
1 answer

Is it undefined behavior to dereference a *mut T cast to *mut ManuallyDrop?

According to the docs, ManuallyDrop is a zero-cost wrapper. Does that mean I can dereference a raw pointer to ManuallyDrop casted from a raw pointer to T?
3
votes
1 answer

CString::new().unwrap().as_ptr() gives empty *const c_char

I have a C function that expects *const std::os::raw::c_char and I have done the following in Rust: use std::os::raw::c_char; use std::ffi::{CString, CStr}; extern crate libc; fn main() { let _test_str: *const c_char = CString::new("Hello…
xxks-kkk
  • 1,743
  • 3
  • 21
  • 39
3
votes
1 answer

Is there a clearer way of representing dereferencing raw pointers and applying their functions in Rust?

I'm working with raw pointers in Rust and I've got a couple of lines which are really hard to read because I can't find an operator in Rust that has a similar function to -> in C++. Does one exist? Here's a snippet of my code in its currently hard…
Harvey Adcock
  • 688
  • 4
  • 13
3
votes
1 answer

How to turn vector of raw pointers into a vector of unique pointers?

#include enum ListOfGameStates { // List of game states }; class GameState() { public: GameStates(); // Initializes protected (global) variables virtual ListOfGameStates run() = 0; protected: //…
user2933244
2
votes
2 answers

What would be the proper implementation of a concept describing a raw pointer (to objects)?

This might be of academic interest only. Because I noticed that my practical example might not be the suitable one to raise that question. Originally I had written something like auto bak_ptr{ptr}; // think of an `int* ptr` to make a backup of an…
yau
  • 477
  • 5
  • 13
2
votes
1 answer

c++ creating cyclically dependent objects with raw pointers

I'm trying to create a connected graph and to perform certain computations on it. To do that, from each node in this graph, I need to access its neighbors and to access its neighbor's neighbors from its neighbor and so forth. This inevitably creates…
1
2 3 4