These are Rust's rules for the Borrow Checker. Isn't the first rule really just RAII from C++?

These are Rust's rules for the Borrow Checker.
Isn't the first rule really just RAII from C++? That would mean RAII is sufficient to stop use after free bugs, right?
And the second rule stops data races, but data races are not necessarily a bug.

Ape Out Shirt $21.68

UFOs Are A Psyop Shirt $21.68

Ape Out Shirt $21.68

  1. 2 years ago
    Anonymous

    >Second, you may have one or the other of these two kinds of borrows, but not both at the same time:
    can you read op?
    absolute state of 4chin nowadays
    2nd rule of only 1 mutable reference prevents UB due to strict aliasing and iterator invalidation

    • 2 years ago
      Anonymous

      >2nd rule of only 1 mutable reference prevents UB due to strict aliasing and iterator invalidation
      >hey look at me! im spitting out random complicated words! im smart!

  2. 2 years ago
    Anonymous

    no. in rust if you try
    let mut foo = String::from("frick op");
    something = bar(&foo);
    else = bar2(&foo);

    it won't compile

    • 2 years ago
      Anonymous

      what the frick are you talking about?
      1. you're missing the let keyword on noth vars
      2. else is a reserved keyword so the last variable name is just wrong
      what does this have to do with what op said?

  3. 2 years ago
    Anonymous

    No. The following is valid C++. Note that the function returns a reference (borrow) to something that's destroyed at end of function.
    int& f() {
    std::vector v{0};
    return v[0];
    }

    • 2 years ago
      Anonymous

      Thanks that makes sense.
      What about the heap? Would forcing return by value on the stack, and RAII on heap avoid use after free bugs?

    • 2 years ago
      Anonymous

      Thanks that makes sense.
      What about the heap? Would forcing return by value on the stack, and RAII on heap avoid use after free bugs?

      Like, what's the use case for returning a pointer to stack memory addresses?
      If a language just totally disallowed returning a pointer to stack memory addresses (but allowed heap for obvious reasons), what exactly would be lost?

      • 2 years ago
        Anonymous

        >what exactly would be lost?
        Nothing. In the example, the reference is pointing to heap though (assuming no small size optimization).

        • 2 years ago
          Anonymous

          Oh I see, I'm used to pure C so I assumed that std::vector would be all on stack (like an array) or all on heap - but the header is on stack and the elements are on heap by default, which seems like a bad idea
          If the object was allocated purely on heap though, there is no problem right? The "name" would fall out of scope, but the object itself would still be alive right?

          • 2 years ago
            Anonymous

            Not him but Rust has stack allocated arrays too, vector is a different data structure.

          • 2 years ago
            Anonymous

            C++ std containers use RAII, which means the heap memory gets deleted (ie freed) right when the "name" exits scope.
            You could replace std::vector with std::unique_ptr (which is just a pointer to heap).

          • 2 years ago
            Anonymous

            [...]
            >You could replace std::vector with std::unique_ptr (which is just a pointer to heap).
            * and the example would work just the same: return a reference to freed memory.

            So if you disallow pointer returns completely, and combine with RAII, then it mimics the first rule for the Rust Borrow Checker right?

          • 2 years ago
            Anonymous

            Pretty much, yes. You still need to be careful while writing copy/move constructors for classes that use heap though, or just use std containers that handle the difficult stuff for you.

          • 2 years ago
            Anonymous

            I see, and the main difference between this and pure C is thus that malloc() would have infinite lifetime unless you manually free (instead of having scope based rules like RAII or Rust), and if you lose reference to the allocated memory without freeing, it just leaks and takes up RAM pointlessly

          • 2 years ago
            Anonymous

            Yes, that's right.
            Long-running C programs usually end up manually doing RAII or allocate resources right at program start.

          • 2 years ago
            Anonymous

            C++ std containers use RAII, which means the heap memory gets deleted (ie freed) right when the "name" exits scope.
            You could replace std::vector with std::unique_ptr (which is just a pointer to heap).

            >You could replace std::vector with std::unique_ptr (which is just a pointer to heap).
            * and the example would work just the same: return a reference to freed memory.

  4. 2 years ago
    Anonymous

    >Isn't the first rule really just RAII from C++?
    lol what? no?

  5. 2 years ago
    Anonymous

    this is why IQfy should never be allowed to comment on the rust programming language

  6. 2 years ago
    Anonymous

    Memory safety janny filters another one.

  7. 2 years ago
    Anonymous

    >having to ask permission to "borrow" resources from your computer

    • 2 years ago
      Anonymous

      built for gcc

  8. 2 years ago
    Anonymous

    lol imagine using a non GC'ed language
    sorry virgins I spend my time actually solving problems instead of keeping track of memory

  9. 2 years ago
    Anonymous

    >exactly one mutable reference
    >no circular references, for your """safety"""
    trash

Your email address will not be published. Required fields are marked *