简体   繁体   中英

Is there a language with RAII + Ref counting that does not have unsafe pointer arithmetric?

RAII = Resource Acquisition is Initialization

Ref Counting = "poor man's GC"

Together, they are quite powerful (like a ref-counted 3D object holding a VBO, which it throws frees when it's destructor is called).

Now, question is -- does RAII exist in any langauge besides C++? In particular, a language that does not allow pointer arithmetric / buffer overflows?

D有RAII,但仍然有指针算术:(但是,你真的不必使用它。请注意让D工作对我来说是一个痛苦,所以我只是说。

虽然不完全是RAII,但Python具有with语句,而C#具有using语句。

Perl 5 has ref counting and destructors that are guaranteed to be called when all references fall out of scope, so RAII is available in the language, although most Perl programmers don't use the term.

And Perl 5 does not expose raw pointers to Perl code.

Perl 6, however, has a real garbage collector, and in fact allows the garbage collector to be switched out; so you can't rely on things being collected in any particular order.

I believe Python and Lua use reference counting.

perl, python (C), php, and tcl are reference counted and have mechanisms to destroy an object once its reference count goes to zero, which can happen as soon as a variable goes out of scope. built-in types are released automatically. user-defined classes have a way to define a destructor that will get called upon release.

there are some edge cases: global variables might not be released until the end and circular references may not be released until the end (though php has recently implemented a gc that handles this case and python 2 added a cycle detector).

Python (the standard CPython, not variants like Jython, Unladen Swallow and IronPython) uses reference counting for its objects.

With that, it also has RAII and (mostly) deterministic garbage collection. For example, this is supposed to work deterministically closing files:

def a():
   fp = open('/my/file', 'r')
   return fp.read()

Note fp.close() is never called. As soon as fp goes out of scope, the object should be destroyed. However, there are some cases where deterministic finalization is not guaranteed, such as in:

  • Something throws an exception and the traceback is currently being handled, or a reference is kept to it (note sys.last_traceback keeps the last traceback)
  • Cyclic references exist in an object, causing the reference count to not go to zero

Therefore, while python theoretically has deterministic finalization, it is better to explicitly close any resources where it's possible an exception (like IOError or the like) could cause the object to remain live.

Vala 's memory management of objects is based on reference counting, and it has RAII (in the sense that it's destructors are called deterministically). The typical use case is to create GUIs, where the overhead from refcounting is often negligible. You can use pointers and bypass the refcounting, for example for interoperability, or if you need the extra performance, but in most cases you can live without pointers. It also does something clever, you can mark references as owned or unowned and transfer ownership, and in many cases it is able to elide the reference counting (if an object does not escape a function, for example). Vala is closely connected to GObject/GTK, so it only makes sense to use if you want to work in that ecosystem.

Another interesting candidate would be Rust . While it also has pointers and garbage collection, both are optional. You can write programs completely with an equivalent of C++'s smart pointers, with guaranteed no leaks, and it supports RAII. It also has a concept of reference ownership, like Vala, but a bit more complex. Essentially, Rust gives you complete control over how you manage memory. You can work at the bare metal level, and could even write a kernel in it, or you can work at a high level with a GC, or anything in between, and most of the time it protects you from leaking memory or other pointer-related bugs. The downside is that it is pretty complex, and since it is still in development things might change.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM