0

I'm working on a (maybe) serious programming language and want to learn about implementing memory management. I want this language to enforce RAII, similar to Rust, but, unlike rust, this language is Object-Oriented and I hope I can implement objects that manage their own memory (like Boxes in Rust). Can anyone go into detail about how Rust handles references to Heap memory?

  • 2
    What exactly is unclear to you about how Rust handles references to the heap? (e.g. could you tell us what you know, and what you think is happening, so we can see how to fill the blanks?) – Theodoros Chatzigiannakis Jan 02 '18 at 17:19
  • 3
    rust is object-oriented... For data types to manage resources you need a way to specify "destructors" (unless the language itself handles resource management). rust uses the `Drop` trait for this (which of course needs to be supported by the language itself). – Stefan Jan 02 '18 at 17:23

1 Answers1

0

I think the most obvious way to implement classes is:

  • Your class variables are implemented as pointers, like in C# and Java.
  • There is a single owner of the object and all class variables have move semantics in order to enforce this, like in Rust.
  • Memory is a resource that needs to be cleaned up, so all class variables, after calling the destructor (if any) of the referent object, also call the deallocation routine of your memory allocator, like in C++.
  • You introduce lifetimes in your type system in order to ensure that lending/borrowing the object doesn't allow any non-owning references to outlive it, like in Rust.
Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104