On Wednesday, 16 July 2014 at 10:13:44 UTC, Chris wrote:
The problem you described above is omnipresent. Objective-C has
autorelease, in plain C programs one has to be careful where
and when to free what, etc. I was wondering after reading your
post, whether it would be possible to have an automatic memory
management system that could help to create a 1-1-match, or if
even the compiler + language features could help to _easily_
achieve that.
Of course. Allocating something on the stack and passing around
pointers to it is safe iff you can guarantee that all the
pointers to it are gone once you leave the stack frame (actually
the variables scope). This is what is currently being discussed
in the parallel thread(s) about borrowing with `scope`.
Maybe the copy approach mentioned earlier points into that
direction.
Anyway, I think you are on to something here. Maybe the
solution is not a hyper-super-waterproof GC algorithm (that
will never be achieved, I guess, in any language), but
eliminating the need to collect the garbage as much as possible.
+1
Borrowing makes it possible to do this safely, and in addition,
enables crucial performance optimizations for reference counting,
when it is needed.