4 ms·
Why would you design a list this way? So many things are wrong here; to name just a few: 1. Pointers work fine, you're not solving any problems here. 2. You ca
by scaredginger 4y ago
Why would you design a list this way? So many things are wrong here; to name just a few:
1. Pointers work fine, you're not solving any problems here.
2. You can't rebind references in C++, so you wouldn't be able to delete nodes
3. Even with this insane approach, why would you use inheritance over a sum type?
4. Extra allocations or statics are needed to hold sentinels.
As for why this wouldn't work in Rust, in addition to its many general problems, the fundamental issue is aliasing. Rust mandates that if a mutable reference to an object is alive, then nothing else references it. Thus, it would only work if your entire list was immutable; this may fit within your definition of a sane API, but it's not what most people would want if they were choosing to use Rust.
- archi42 4y agoI was just wondering how to do that without using 'unsafe' rust. 1. Dereferencing raw pointer is unsafe; but maybe not necessary? 2. `int main (void) { int a = 1; int b = 0; int &ref = a; ref = b; return b; }` 3. union access is unsafe 4. the cost of not having nullptr or similar Obviously a linked list is trival to implement with pointers; but at least in C++ a naive linked list implementation can easily produce lots of suffering (read: UAF or DF) with a `delete list.getPointerToElement(i)` (in practice it will be a more convoluted variant of that, maybe introduced by dozens of people working on a badly documented code base over a decade or two). I'd expect if programmers already do that in C++ there isn't much that prevent them doing that in unsafe rust? > insane approach I can assure you my mental health is pretty good, thanks. Though I made a comment regarding a thought experiment in a rust thread, I can see why you would think that.
- scaredginger 4y agoI feel sorry for your coworkers