Questions tagged [ownership-semantics]

Ownership semantics are a set of rules that govern the lifetime of allocated resources. Ownership semantics determine when and how allocated resources can be freed, and whether ownership can be shared.

67 questions
2
votes
1 answer

Do Rust collections have the ability to own the data they store?

Given the following code (which does not compile): fn main() { let mut v = vec!(); { let name = "Bob the Builder".to_string(); v.push(&name); } for m in &v{ println!("{}", m); } } I have created a…
Ralph Caraveo
  • 10,025
  • 7
  • 40
  • 52
2
votes
1 answer

Can Rust consume an iterator passed into a function?

I'm trying to implement a simple REPL calculator in Rust and I'm hitting brick walls all over the place. I'm consuming chars while iterating over a hardcoded string. When I hit a numeric character I want to pass control over to a function that will…
neektza
  • 483
  • 6
  • 11
2
votes
2 answers

How to manage object life time using Boost library smart pointers?

There is a scenario that i need to solve with shared_ptr and weak_ptr smart pointers. Two threads, thread 1 & 2, are using a shared object called A. Each of the threads have a reference to that object. thread 1 decides to delete object A but at the…
red.clover
  • 1,788
  • 2
  • 18
  • 32
1
vote
1 answer

How do I model a non-owning view on some resource (not itself managed via shared_ptr), if not via raw pointer?

std::unique_ptr is for exclusive ownership. std::shared_ptr is for shared ownership. So it looks like a raw pointer is left just the job of being a non-owning pointer, at least in good code. But if I see a raw pointer somewhere, I actually don't…
Enlico
  • 23,259
  • 6
  • 48
  • 102
1
vote
1 answer

Pass ownership of an object into method of the same object?

I came across come C++ code similar to the following (more or less minimal) example. Please consider the marked method call in the function on the bottom: #include static unsigned returnValue = 5; void setReturnValue(unsigned u) {…
HelpingHand
  • 1,294
  • 11
  • 27
1
vote
1 answer

Sharing arrays between threads in Rust

I'm new to Rust and I'm struggling with some ownership semantics. The goal is to do some nonsense measurements on multiplying 2 f64 arrays and writing the result in a third array. In the single-threaded version, a single thread takes care of the…
pveentjer
  • 10,545
  • 3
  • 23
  • 40
1
vote
3 answers

Moving objects can change the order destructors are called. Why isn't that a problem?

In the following example, a mutex is used to protect a file: #include #include #include std::mutex m; int main() { std::unique_ptr> ptr_1 =…
1
vote
1 answer

Fire event at changing refcount in PHP

When refcount = 0, the __destruct magic method is called. Is there any way to catch the fact that refcount is decreased or increased, but not zero? A use-case for such an event is to detect if an object is being shared (refcount > 1) or owned…
Olle Härstedt
  • 3,799
  • 1
  • 24
  • 57
1
vote
1 answer

Immutable Strings and Cloning

I have the mindset keeping my Strings immutable, a single source of truth. As I take the same mindset into Rust, I find I have to do a lot of cloning. Since the Strings do not change, all the cloning is unnecessary. Below there is an example of this…
NebulaFox
  • 7,813
  • 9
  • 47
  • 65
1
vote
1 answer

Movable but non-copyable objects: passing by value vs by reference?

Considering only objects that are movable but non-copyable (e.g., std::thread or std::unique_ptr), I want to transfer the ownership of the resource such an object contains by passing it as an argument to a constructor. I'm comparing two approaches:…
JFMR
  • 23,265
  • 4
  • 52
  • 76
1
vote
2 answers

calling functions which take unique_ptr object

I have a function in which the callback receives an event holding a unique_ptr instace of data. I can retrieve the char* through event.data.get() which should give me the pointer but not ownership allowing the object to freely delete it. Now I have…
Irelia
  • 3,407
  • 2
  • 10
  • 31
1
vote
2 answers

How to organize object ownership for class that lives lesser time than owner of the object?

I have the following situation: there is class of GraphicsContext: class GraphicsContext { ... private: std::unique_ptr m_renderer; } And there is a class of application that uses the GraphicsContext: class Application { …
1
vote
1 answer

Swift unowned self leaking when 'owned' by a view being presented

I am experiencing a leak with unowned self under conditions where, to the best of my knowledge, there shouldn't be a leak. Let me show an example, it is a little contrived, so bear with me, I've tried to make the simplest case I could. Suppose I…
1
vote
1 answer

Why does the Rust book present assigning a variable to another as copying the top-level structure?

In the section on ownership in The Rust Programming Language, Strings are represented as a structure with 3 fields (with one of the 3 fields being a pointer to the actual byte vector). There is an example: let s1 = String::from("hello"); let s2 =…
Stefan
  • 27,908
  • 4
  • 53
  • 82
1
vote
1 answer

What is the difference between dereferencing a raw pointer to a String and a raw pointer to an i32?

fn func(s: *mut String, a: *mut i32) -> usize { println!("{}", unsafe { *s }); println!("{}", unsafe { *a }); unsafe { (*s).len() } } fn main() { let mut s = String::from("hello"); let mut a = 10; func(&mut s, &mut…
soupybionics
  • 4,200
  • 6
  • 31
  • 43