Is there any difference between null and 0 when assigning to pointers in unsafe code?

Posted by Eloff on Stack Overflow See other posts from Stack Overflow or by Eloff
Published on 2010-05-05T00:24:02Z Indexed on 2010/05/05 0:28 UTC
Read the original article Hit count: 505

Filed under:
|

This may seem odd, but in C (size_t)(void*)0 == 0 is not guaranteed by the language spec. Compilers are allowed to use any value they want for null (although they almost always use 0.)

In C#, you can assign null or (T*)0 to a pointer in unsafe code.

  1. Is there any difference?
  2. (long)(void*)0 == 0 (guaranteed or not? put another way: IntPtr.Zero.ToInt64() == 0)

MSDN has this to say about IntPtr.Zero:

"The value of this field is not equivalent to null." Well if you want to be compatible with C code, that makes a lot of sense - it'd be worthless for interop if it didn't convert to a C null pointer. But I want to know if IntPtr.Zero.ToInt64() == 0 which may be possible, even if internally IntPtr.Zero is some other value (the CLR may or may not convert null to 0 in the cast operation)

Not a duplicate of this question

© Stack Overflow or respective owner

Related posts about c#

Related posts about unsafe