Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are pointers reference types?

Tags:

This week I've been working on some reflection-based code, and during unit testing found an unexpected condition: pointers are reference types. The C# code typeof(int).MakePointerType().IsClass returns true.

I checked in my just-arrived Annotated CLI Standard, and sure enough, pointers are clearly defined as reference types.

This was surprising to me, coming from a C++ background. I had just assumed that pointers would be value types.

Is there a particular reason why pointer types are reference types and not value types?

Update (clarification)

When talking about pointers and references, things often get confusing regarding the "pointer" and "pointee". So here's some clarification.

Types can be reference types or value types, but variables are a bit different. (Sorry, I haven't had a chance to read through my CLI Standard, so the terminology and concepts may be wrong - correct me, please!)

Given this code (local variable concepts for reference types):

var x = new MyClass();
var y = x;

The variables x and y are not actually reference types, but they're references to an object that is a reference type (MyClass is a reference type). In other words, x and y are not instances of the reference type; they only refer to an instance of reference type.

Given this code (local variable concepts for value types):

var x = 13;
var y = x;

The variables x and y are instances value types (or at least act like they're instances).

So then we come to this code:

var i = 13;
var x = &i;
var y = x;

If the pointer type is a reference type, then this is how I interpret the statement x = &i:

  1. An instance of type int* is created, pointing to i.
  2. Since pointers are reference types, this instance is created on the heap (assuming that all reference types are placed on the heap, an implementation detail).
  3. x is a reference to this pointer instance.
  4. The pointer instance will eventually be garbage collected, just like other reference types.
  5. When y = x is executed, the reference is copied. Both y and x refer to the same instance of the pointer object.

Perhaps I'm completely wrong in this interpretation.

Coming from a C++ background, it would make more sense to me for pointers to be value types, so the statement x = &i is just assigning the address of i to the instance of the value type x, and y = x copies that address value into y. No "pointer object" would be created on the heap.

like image 676
Stephen Cleary Avatar asked Jul 23 '10 11:07

Stephen Cleary


People also ask

Are pointers reference types?

References are not a kind of pointer. They are a new name for an existing object.

Why are references different from pointers Mcq?

What is the difference between references and pointers? Explanation: References are an alias/another name for a variable whereas pointer stores the address of a variable. Pointers need to be deference before use whereas references need not.

What is a pointer referencing?

A pointer in C++ is a variable that holds the memory address of another variable. A reference is an alias for an already existing variable. Once a reference is initialized to a variable, it cannot be changed to refer to another variable. Hence, a reference is similar to a const pointer.

Why do pointers have types?

The Data type is needed when dereferencing the pointer so it knows how much data it should read. For example dereferencing a chart pointer should read the next byte from the adress it is pointing to while an int pointer should read 2 bytes.


1 Answers

I haven't seen a complete answer, so I'm closing this question down for now.

I'm forced to conclude: pointers (e.g., int *) are actually value types; the fact that their Types return true for IsClass is a mistake in the specification. I suspect that no one has noticed this because getting the type of a pointer is a very rare operation.

like image 131
Stephen Cleary Avatar answered Sep 27 '22 20:09

Stephen Cleary