View Issue Details

IDProjectCategoryView StatusLast Update
0000668LDMudLPC Languagepublic2009-08-14 10:23
Reporter_xtian_ Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Summary0000668: passing by reference: unclear pointer/reference semantics
DescriptionIm am unsure if this is simply not explained enough in the documentation or a language problem: There is a difference in the way variables that are passed by reference by themselves (mappings, structs, ...) are handled when they are implicitely or explicitely passed by reference.

example:

void delete(mapping m)
{
  m= 0; // what does this delete?
}

void create()
{
  mapping n= ([1:2]);

  delete(n); // this will not delete n (pointer-like semantics)
  printf("%O\n",n);
  delete(&n); // this will delete n (reference-like semantics)
  printf("%O\n", n);
}
Additional Informationnote that:
  object ob= ...; // any object
  delete(&ob); // ob= 0

does _not_ destruct the object, which is probably good. But this special case should be mentioned in the documentation somewhere.
TagsNo tags attached.
External Data (URL)

Activities

Gnomi

2009-07-28 02:26

manager   ~0001241

The difference is because only the mapping is implicitly passed by reference, not the variable pointing to the mapping. So changes in the mapping itself are globally visible. But changes to the variable (putting another value in there, even if it's new mapping) are not.

_xtian_

2009-07-30 09:43

reporter   ~0001243

Yes, I understand the implementation details. But my point is that these are not clear at first glance, even for people that use C++ for example. You have to try it out to understand how the language/driver behaves.

One the one hand I find that this is a bit too convoluted and technical for a language like LPC which, I find, should especially target unexperienced programmers and be as newbie-safe as possible. (but it is probably too difficult to introduce a new behaviour without breaking code all over the place)

On the other hand then, this behaviour should at least be well documented.

So I guess I am asking: Do you think this is more or less error prone for a user than any other way? (while it probably is better this way, since changing it will be difficult anyhow) And then could we document this behaviour better?

fufu

2009-07-31 12:42

manager   ~0001245

void delete(mapping m)
{
  m= 0; // what does this delete?
}

This does not delete anything really, not even if m is passed by reference:

{
   mapping n = ([ 1:2 ]);
   mapping m = n;
   delete(&n);
   // now n is 0, but the mapping is still accessible through m
}

What happens in your example is that by assigning 0 to m, the mapping is no longer accessible, and its reference count drops to 0, causing it to be freed.

Objects on the other hand are not destructed when they are not accessible through any variables - they can still be found by their name after all, and by other means. Also, unlike simple values, objects can be active - there can be a heart_beat, pending call_outs, and so on.

I find this quite intuitive - which part of the documentation would you like to have clarified? Where does the idea that the assignment deletes anything come from?

Does it come from Java? But even in Java, o = null; does not delete anything; it may cause objects to become unreachable though, and consequently be garbage collected.

_xtian_

2009-08-14 10:23

reporter   ~0001246

The thing is that typical LPC users dont think about how their data is represented in hardware and dont have a concept of pointers and memory adresses. To be honest: You can only understand the above issues if you have an understanding of C++ or other real life programming languages.

Suggestion for an addendum to the manpage:

Subtleties concerning references:
--------------------------------
As has been explained above, performing any operation on an argument that has been passed by reference to a function will modify the data that has been referred to, effectively "changing it for everybody".

But this does not hold true for assignements (as in x= 123) to references. Assignements do not operate on the referenced data, but on the reference itself. The original data is not modified. The target of the reference is.
This means that for explicit argument references (invoked by using the & ) the target of the reference is changed locally and for the caller. And that for implicit (automatical) referencing for mappings, arrays, structs or objects, which is the normal usage, the target of the reference is changed locally only.

If then the data is not accessible to any other program any more, the driver will free the system memory for it, albeit this may not happen immediately. Also, assigning a zero to an object will not delete it, only calling destruct() will.

Issue History

Date Modified Username Field Change
2009-07-28 01:59 _xtian_ New Issue
2009-07-28 02:26 Gnomi Note Added: 0001241
2009-07-30 09:43 _xtian_ Note Added: 0001243
2009-07-31 12:42 fufu Note Added: 0001245
2009-08-14 10:23 _xtian_ Note Added: 0001246