C# default value of a pointer type
Asked Answered
Q

3

14

I have been searching through the C# language spec and I can't find anything which says whether a pointer type (e.g. int*) gets initialized with a default value. I created a simple test app and it appears to initialize them to zero but I'd like to confirm this with the spec.

I started looking for this because I noticed in reflector the IntPtr class uses this code to define its IntPtr.Zero:

public struct IntPtr : ISerializable
{
   private unsafe void* m_value;
   public static readonly IntPtr Zero;

   .......

   public static unsafe bool operator ==(IntPtr value1, IntPtr value2)
   {
       return (value1.m_value == value2.m_value);
   }

   ........
}

which means that when you compare against IntPtr.Zero it actually is comparing against the default value assigned to the m_value field which has type void*.

Thanks.

Quotation answered 6/2, 2015 at 13:21 Comment(3)
There are parts of the spec, discussing pointers, which discuss how they interact with definite assignment analysis. Given that the whole point of definite assignment analysis is to avoid you observing an uninitialized state of a variable, maybe the answer is Mu. Also, not everything you see in the implementation of framework types is the full story - sometimes special handling by the runtime produces the actual behaviour.Pinelli
Well spotted... looks like they missed it when writing the spec. Guess you can assume it's zero (null) :-)Lorielorien
What's confusing is that CIL defines pointer types as a subset of reference types, but C# defines pointer types as a distinct class of types. So when CIL specifies that reference types have a default value of null, that means more than when C# specifies the same thing.Palliasse
T
2

I believe pointers have no default value; due to a pointer's value being the address of a portion of memory containing something you assign it to. If you haven't assigned it, it could be pointing to anything in memory.

Perhaps the CLR's default behavior is to set it to IntPtr.Zero, which "represents a pointer or handle that has been initialized to zero", which looks to be likely from Carmelo Floridia's answer. This seems to be an implementation detail which the spec may not have elaborated upon.

Tabret answered 6/2, 2015 at 14:25 Comment(0)
C
1

Default value of pointer types by specifications is null. This is following from two facts.

First fact is that pointer is reference type. It is described in ECMA-335: enter image description here

Second fact is that reference-type has default value - null.

Cassey answered 15/12, 2015 at 17:47 Comment(2)
Type.IsClass on pointers is true but default(typeof(void*)) is IntPtr.Zero, not null. Either reference types do not have default value as null always or pointer is not a reference type. I believe it's the former.Rights
@Rights IntPtr.Zero initialized by default. First field IntPtr.Zero is private unsafe void* m_value. If void * m_value is IntPtr.Zero then we have similarity of recursion.Cassey
R
-1

Using the Visual Studio debugger you can get the default value assigned to an int pointer. In a 32 bit architecture it is 0x00000000 (32 0 bits).

class Program
{
    private static unsafe int* m_value;
    static void Main(string[] args)
    {
    } // <<== Break point here
}

Watch Window
_____________________________________________
Name    Value       Type
======= ==========  ========
m_value 0x00000000  int*
Remittee answered 6/2, 2015 at 13:49 Comment(2)
Yes as I said I wrote a test app which did this but this doesn't necessarily imply that the value will always be zero (it could just be luck).Quotation
The C# specification very carefully does not define the defualt value for a pointer.Ripen

© 2022 - 2024 — McMap. All rights reserved.