5 ms·
Why would a programmer ever worry about "ownership"? I've never had to do that.
by mikewarot 6y ago
Why would a programmer ever worry about "ownership"? I've never had to do that.
- mhh__ 6y agohttps://xkcd.com/371/ https://xkcd.com/371/
- hhas01 6y agoAlso: https://xkcd.com/927/ https://xkcd.com/927/
- krisoft 6y agoIt's a memory safety concept. In languages like C you allocate memory yourself, and then you have to make sure to free it up too. This is error prone. In garbage collected languages like Java there is a garbage collector who figures out when is a piece of memory no longer in use. This has run-time cost. The third possible way is to make the ownership of that part of the memory explicit. This way the compiler can check memory safety at compile time. There is a much better description of this in the Rust book: https://doc.rust-lang.org/book/ch04-01-what-is-ownership.html https://doc.rust-lang.org/book/ch04-01-what-is-ownership.htm...
- freeone3000 6y agoGarbage collected languages (Java, JS) have significant overhead, and unpredictable latency spikes. Scope-based memory management (C++) has copying overhead and destruction overhead, and a whole lot of complexity to avoid those. Ownership-based memory management removes most of the overhead associated with copying and destruction in a less-complex fashion.
- ben-schaaf 6y agoNote C++'s semantics are akin to ownership, it just has different defaults and none of the safety guarantees. A std::vector is owned by the current scope, ownership can be passed around using std::move and references can be taken using &.
- rwmj 6y agoBad garbage collector implementations, but not GC in general. malloc/free can have latency spikes too. Also not every programming problem in the world is a memory-constrained hard real time problem - in fact, I would say almost all of them are not. Memory ownership is certainly an interesting thing to have in your arsenal, but a whole class of problems (especially anything with a graph or modestly complex interlinked data structure) can't easily be reasoned about using this model, or you end up copying stuff anyway to get around the borrow checker. Having said all that, I'd really like to see a language that lets you use a garbage collector 99% of the time for ease of use, but allows you to use other models for the small sections of the code which need it or within certain designated real time threads.
- mhh__ 6y agoD let's you do that if you want.
- megameter 6y agoI beg to differ. For a sufficiently large time scale, every programming problem is a memory-constrained hard real time problem.
- vips7L 6y agoYou're looking for D! https://dlang.org/spec/function.html#nogc-functions https://dlang.org/spec/function.html#nogc-functions
- mhh__ 6y agoOwnership is a conservative restriction of allowed behaviours (i.e. rust still has ref counts), the three approaches don't do exactly the same job
- ben-schaaf 6y agoThen you haven't ever written a line of C/C++ I presume? Ownership is just a formalization of the way almost all resources (including memory) are managed in C: A single function has the responsibility of doing cleanup; that responsibility can be moved around to other functions of course, but only one ever has that responsibility at a time.
- mikewarot 6y ago>Then you haven't ever written a line of C/C++ I presume? Correct... I'm a Pascal programmer. I create objects when a program starts, delete them at the end. I've never really had any of the pointer problems that seem to plague C/C++.
- wwright 6y agoI assume you write fairly "static" software that knows its job pretty well in advance. Ada programs tend to use a similar allocation strategy. A lot of programs written in C and C++ are very dynamic, and may need to massively increase or decrease the number of objects they use while running. For example, consider an audio workstation: when you first start it, it has very little to do, but the user can add literally hundreds or thousands of tracks, each with dozens of effects and modifications. Each of these features is composed of many objects. Ownership comes in when these objects have complex relationships, where one object may need to be referenced by many others. When it is time to clean up some of these objects — for example, when a user deletes a track in the audio workstation — you have to know which of these shared objects to delete. Ownership is one way of modelling that problem. Languages that talk about ownership try to optimize the "best case" where an object has exactly one owner, because then it's very simple for the compiler to free that object. The trick is that you have to prove that no one else is using it, or else you have memory errors.
- pwdisswordfish4 6y ago> Correct... I'm a Pascal programmer. I create objects when a program starts, delete them at the end. Then I guess you should instead say you’re a ‘single-shot, compiler-like batch job executable’ programmer. I imagine that colours your perception more than the choice of programming language itself. If you were to write a program that has to manipulate deeply nested data structures, or a long-running service that has to acquire and dispose of an unbounded number of various kinds of resources over the course of its runtime in a deterministic manner, you’d start longing for an ownership system very quickly.
- ycombonator 6y agoValid question. if you started programming dynamic languages like Python you don’t have to worry about memory control.
- kzrdude 6y agoIt's an object lifecycle concept. You worry about ownership if you have a resource and need to think of when to free/dispose/close it. You worry about ownership if you have an object that many have a reference to, and some of the references shouldn't keep the associated resources alive, and some other of the references should keep it alive. Those that keep things they referenced alive are (shared) owners.
- epage 6y agoSo everyone else is focusing on the memory allocation side of ownership. The other side to this is aliasing / borrowing / handling references and mutability. In Python, you might have a `list` that you built up and them pass its data to multiple functions. You assume these functions are do not mutate their arguments and you keep passing the same `list` over again. In this case, the caller is controlling the invariants of the `list` and therefore "Owns" it. Deep down, one of those functions needs to mutate a `list` to do its calculations. Where the mutation is happening, it assumes it is the "Owner" and can modify the `list` without violating any invariants. Unfortunately, a "Borrowed" list was passed in and now the caller's invariants are violated. Rust's ownership model helps to catch this kind of problem at compile time. By default, everything is single-owner though you can change that with a `Mutex`.
- creata 6y agoYep. Even when you're doing something as simple as iterating over some data structure, you have to remember not to mutate it. Rust enforces that, but Python just either corrupts the data structure / iterator, or if you're lucky it'll throw an exception.