Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is null reference basically all-zeroes pointer in C#?

Tags:

c#

.net

memory

All object references in .NET are basically 32-bit or 64-bit pointers.
I am curious about how null is represented in memory. Is it actually 32 or 64 zeroes? Is it guaranteed to be so, according to specification?

I couldn't find the answer on MSDN or C# language specification.
There is also a similar question for C language, but what about C#?

like image 458
Yeldar Kurmangaliyev Avatar asked Aug 13 '18 11:08

Yeldar Kurmangaliyev


People also ask

Is a null pointer zero?

A null pointer constant is an integer constant expression that evaluates to zero. For example, a null pointer constant can be 0, 0L , or such an expression that can be cast to type (void *)0 . You can specify any of the following values for a null pointer constant: 0.

IS null always defined as 0 zero )?

The null pointer constant is always 0. The NULL macro may be defined by the implementation as a naked 0 , or a cast expression like (void *) 0 , or some other zero-valued integer expression (hence the "implementation defined" language in the standard). The null pointer value may be something other than 0.

IS null a pointer in C?

Master C and Embedded C Programming- Learn as you goA null pointer is a pointer which points nothing. Some uses of the null pointer are: a) To initialize a pointer variable when that pointer variable isn't assigned any valid memory address yet.

How do I know if a pointer is pointing to null?

int * pInt = NULL; To check for a null pointer before accessing any pointer variable. By doing so, we can perform error handling in pointer related code e.g. dereference pointer variable only if it's not NULL.


1 Answers

I couldn't find the answer on MSDN or C# language specification.

Bacause it does not matter. As you never can work with the pointer - this is an implementation detail.

You can check the CLR sources at https://github.com/dotnet/coreclr

THAT SAID - I think it is all 0 because when laoding that into a register, there should be (at least it was wehen i learned assembler) a special flag that the value is null, so the check is super fast.

like image 91
TomTom Avatar answered Oct 30 '22 08:10

TomTom