lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Jun 8, 2015 at 12:48 AM, Nagaev Boris <bnagaev@gmail.com> wrote:
> On Mon, Jun 8, 2015 at 2:31 AM, Coda Highland <chighland@gmail.com> wrote:
>> The thing is, object references in C++ were actually explicitly
>> DESIGNED to address the problems with pointers. They actually DON'T
>> have substantially different semantics from the object itself. You
>> can't reassign an object reference point to a different instance; if
>> you assign to an object reference, it behaves exactly the same as
>> assignment to the original object. Accessing the elements of an object
>> reference has the same semantics as accessing the elements of the
>> object itself. You know that there's exactly one object on the other
>> side of the reference; there's no ambiguity over whether it's 0
>> (null), 1 (pointer), or many (array). You don't have to worry about
>> the memory management of a reference.
>
> Disagree. If a pointed object is deleted, than a reference becomes a
> dangling reference (not better than dangling pointer). By the way,
> Rust developers say this problem is addressed in Rust.

Well sure, it's not an especially difficult problem to solve if you're
building a new programming language, but it comes at a cost. You
sacrifice some performance (Rust doesn't consider being as fast as C++
to be a goal) and you either have to give up some expressiveness
(nulls are important, especially when dealing with foreign functions)
or include a way to deal with unsafe pointers -- note that this is
what Rust does: it IS possible to get a null/dangling pointer is Rust;
you just have to go out of your way to do so.

And so it is in C++: If you've got a dangling reference to an object,
you're doing something wrong in the first place. References are
intended to be temporary. If you think that there's any chance
whatsoever that the object might get deleted, you shouldn't be using a
reference in the first place -- you CERTAINLY shouldn't be storing a
reference in an instance variable unless you know PRECISELY what
you're doing. And if you're not doing anything stupid yourself, then a
dangling reference is a sign of a bug in the code you're invoking, and
no amount of paranoia or language features can protect you from that.

/s/ Adam