4

C++ code:

person* NewPerson(void)
{
  person p;
  /* ... */
  return &p; //return pointer to person.
}

C# code:

person NewPerson()
{
  return new person(); //return reference to person.
}

If I understand this right, the example in C++ is not OK, because the p will go out of scope, and the function will return a wild pointer (dangling pointer).

The example in C# is OK, because the anonymous new person will stay in scope as long as there is a reference to it. (The calling function gets one.)

Did I get this right?

Palec
  • 12,743
  • 8
  • 69
  • 138
Niklas
  • 1,753
  • 4
  • 16
  • 35

7 Answers7

4

The example in C++ is not ok because the 'p' will go out of scope, and the function will return an invalid pointer.

Correct.

The example in C# is ok because the anonymous 'new Person' will stay in scope as long there is any reference to it.

That is more or less correct but your terminology is not quite right. Scope in C# is the region of text in which an unqualified name can be used. The object here does not have a name. Lifetime is the period of runtime during which a storage location is guaranteed to be valid. Scope and lifetime are connected; when control leaves code associated with a scope, the lifetimes of locals declared within that scope are usually permitted to end. (There are situations where lifetimes of locals are longer or shorter than the time when control is in their scope though.)

Also, note that it is not any reference to the Person object that keeps it alive. The reference has to be rooted. You could have two Person objects that reference each other but are otherwise unreachable; the fact that each has a reference does not keep them alive; one of the references has to be rooted.

Eric Lippert
  • 647,829
  • 179
  • 1,238
  • 2,067
2

The scoping rules in this example are analogous but in C# if the returned value is assigned to something then it will not be garbage collected as long as something holds a reference to it. If it's not assigned to something, nothing holds a reference to it and it will be garbage collected next time the collector executes

Rune FS
  • 21,497
  • 7
  • 62
  • 96
2
person* NewPerson(void)
{
  person p();
  /* ... */
  return &p; //return pointer to person.
}

p is not a person, see most vexing parse. As such, you'd get a compiler error.

For the rest, yes you're right.

Community
  • 1
  • 1
Xeo
  • 129,499
  • 52
  • 291
  • 397
1

In C++, that 'p' will live on the stack and thus get clobbered when the function returns. In C#, the garbage collector knows to not clobber it until the last reference is lost.

('clobber' being used loosely here... :p)

Corbin
  • 33,060
  • 6
  • 68
  • 78
  • Yes i dont understand clobbered :) – Niklas Dec 10 '11 at 12:17
  • I should have been more technical :). I just meant that it no longer exists in a validly accessible way. Until it gets over written, the object will still exist in the place where it was, but once the variable goes out of scope (in the C++ version), there is no guarantee (and it actually becomes very, very likely) that the object will very soon be overwritten. – Corbin Dec 10 '11 at 12:19
1

Yes, you got it right.

However, in C++ you would really do like this

person NewPerson()
{
  person p;
  /* ... */
  return p; //return person.
}

and be pretty sure that in a call

person x = NewPerson();

the compiler will optimize out the copying of the return value.

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • OK, but it will not be ompimized in speed, right? Because a pointer is more quick to return? (or maybe thats the good option in c++ do make factoring functions) – Niklas Dec 10 '11 at 12:20
  • In this particular case the language specifically allows the compiler to omit the copying of the return value and construct it in place in `x`. Most every compiler takes advantage of this. – Bo Persson Dec 10 '11 at 12:25
  • ok, so it will have the same speed, as if you returned a pointer to a new person? – Niklas Dec 10 '11 at 12:34
0

Did i get this right?

Yes.

BTW: in C++ person p(); declares a function and will not call the default ctor of person. Just write person p;

Simon
  • 1,496
  • 8
  • 11
0

This will not work in C++ because you are returning a reference to a temporary which will be destroyed once the function is over. You need to create a new person on the heap and then return a reference to that.

Ell
  • 4,238
  • 6
  • 34
  • 60
  • Beside the mistake with person p() instead of person p - p is not a temporary object (in the C++ wording). – Simon Dec 10 '11 at 12:09
  • Sorry you are correct - I feel like down-voting my own answer now :O – Ell Dec 10 '11 at 12:11