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.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr. Stroustroupe.
C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.
C is more difficult to learn than JavaScript, but it's a valuable skill to have because most programming languages are actually implemented in C. This is because C is a “machine-level” language. So learning it will teach you how a computer works and will actually make learning new languages in the future easier.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With