views:

376

answers:

6

In C++ a pointer is a pointer to an address of memory where another variable is stored and in C# a reference is some how same. What is the difference between these two?

+4  A: 

In C# the reference type will be automatically garbage collected when no longer needed.

Darin Dimitrov
A: 

The first difference is that you cannot use pointer arithmetic on references. Otherwise, they're pretty much the same.

  • Both point/reference to an actual object.
  • Both can be NULL/null (i.e. not pointing to a valid object)
  • Both can be assigned a new object

Another difference is that the Garbage Collector takes care of the allocated resources in C#.. in C++, you have to delete them yourself (or use a smart-pointer).

And then, of course... you don't have to dereference references manually in C# to access the value.

Christian
oO Why the downvote?
Christian
A: 

C++ is physical-level pointer where it stores the value of physical address. C# reference is rather a hidden or opaque pointer where u just know it points to some object.

In C++ you work with pointers either on physical level - memory operation or use it access the objects it points to.

// physical level operation

// set pointer to specific address
byte *pPointer = 0x00LB10;
// increment pointer
pPointer++; 

// OR
// dereference operation for classes and structure
pPointer->MyProperty

In C# you can only use reference (reference type object) to access objects.

MyReference.Property
Captain Comic
+1  A: 

The so-called references in C# are implemented by a pointer to a pointer to the actual object. They're called handles and while the handle itself stays fixed throughout the life time of the program, the internal pointer changes as the object gets moved in memory by the garbage collector during the memory compacting phase.

Blindy
+1  A: 

Those are different beasts. In C++, boost::shared_ptr is somewhat similar to C# referenecs, but is also a distinct beast (of these two).

Reference points to an object which will be disposed by a garbage collector. Same happens in case of shared_ptr. Yet, there's a conceptual difference between how C# and boost::shared_ptr track object lifetimes (see below).

C++ pointers may point to anything, allocated either on stack (in which case, disposing is not needed), statically on heap, or something allocated manually, on-demand, which then has to be freed and it's totally on programmer's responsibility.

Differences between shared_ptr and C# references:

shared_ptr is a reference counter. It counts how many clones of your pointer exist, and disposes the object if there's none. This does not provide a solution against cross-references (or circular references) when A points to B which points to A, but both A and B are no longer needed.

C# garbage collection deals with that. C# does not count references. Instead, it maintains an accessibility list - what can be accessed thru all open pointers we have. What cannot, is disposed. This approach is safer, but has several disadvantages in terms of performance predictability.

Pavel Radzivilovsky
+2  A: 

For me, the concept is the same, or at least intended to be the same, but the actual use given, is not.

When we talk about pointers or reference we are talking about a concept. This variable "points to" or its a "reference" to something else, something that is stored somewhere else.

If I use that reference, for example by updating the value, I'm actually updating the referenced object, the object the variable points to. If I have an object referenced from two different places, I'm actually update the same thing from both places.

So the concept can be related to your address in the real world, is just a way to refer to other thing, and that reference can be shared, like many people having my address.

The difference is more on how it has been used and what's been allowed by the language. Note that the implementation details of the reference or pointer are not implicit in the concept itself. In C# that continues to be true.

A reference is a reference and being that so, it just does not make sense to sum two reference, or to add one to a reference ... what would that even mean? I can't add two home addresses (like Baker Street + Home Street) ...

In C++ the implementation detail of the reference is actually the reference. That way a pointer is not just a pointer, its also (and is used as) a memory address. That way, for example, you can add to pointers, because it actually makes sense to add two memory addresses to obtain another one, or to add 4 to a memory address. So in C++ a pointer stopped being a concept an became a name given to an actual implementation detail.

That is why, in C#, pointers and reference are different things. Because, through semantically the meaning is quite similar, they wanted to differentiate from the old concept of C and C++, where a pointer had became the same as a memory address.

I recommend you read the article "References are not addresses" from Eric Lippert where I believe he does a great job explaining the differences.

Jorge Córdoba
You absolutely cannot add two pointers in C++.
FredOverflow