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

81 questions
42
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
  • 6,003
  • 11
  • 53
  • 90
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
  • 19,566
  • 15
  • 85
  • 160
8
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,588
  • 5
  • 18
  • 37
6
votes
1 answer

Why do most ffi functions use raw pointers instead of references?

Both in ffi tutorials and in automatically generated interfaces, *const T pointers are used most of the time. As far as I know the difference between &T and *const T is only that *const T doesn't have to fulfill certain conditions like not being…
Goldenprime
  • 324
  • 1
  • 10
6
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,310
  • 1
  • 9
  • 13
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,544
  • 2
  • 17
  • 33
5
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
  • 982
  • 11
  • 28
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,314
  • 1
  • 10
  • 13
4
votes
0 answers

Initialize sized members of DST in Rust (on the heap)

I'm trying to implement a chunk list a.k.a. a linked list where each node contains multiple elements, a tradeoff between a linked list and a Vec. The intuitive way to do this is: pub struct Node { next: /* ref to next node */, data:…
Asone Tuhid
  • 539
  • 4
  • 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
  • 921
  • 1
  • 8
  • 20
4
votes
1 answer

C++ working with objects on heap

I am currently learning C++, coming from C#/Java background, using visual studio 2017. I have a question in regards to creating objects on heap and referencing them properly down the road. So far I came across multiple tutorials and ways of doing…
AleksanderNaumenok
  • 161
  • 1
  • 1
  • 7
3
votes
2 answers

Polymorphism : raw pointer vs smart pointer

how makes this work ? I want to use a vector of multiple types (research, add, delete) for an inventory management (Potions, Weapons, etc.. all derived from virtual class Item). I simplify the problem here : I have a vector containing Item (Base…
3
votes
2 answers

How to prevent deletion of a raw pointer that is used as shared pointer?

I implemented a C-API for a C++ class which uses shared-pointers of other objects to access them. In my C-API I can of course only get raw pointers. So I "convert" the raw pointer in my C-API to a shared-pointer and use this then with my C++ class…
3
votes
1 answer

Rust creating String with pointer offset

So let's say I have a String, "Foo Bar" and I want to create a substring of "Bar" without allocating new memory. So I moved the raw pointer of the original string to the start of the substring (in this case offsetting it by 4) and use the…
Dan
  • 83
  • 9
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…
1
2 3 4 5 6