views:

402

answers:

8

(I know what null is and what its is used for)

Question: OK, say we make a reference to an object in whatever language. The computer makes a little 32-bit (or other size, depending on computer's design) space in memory for that reference. That memory can be assigned to a value that represents an object's location in memory. But when I set the reference to null, what value does it really have? (what are the individual bits in the reference set to) Are the bits just zeroed out? But wouldn't that also be a location in memory? How does the computer tell that the reference contains null instead of a reference to an object?

I know this isn't an "important" question, but I'm curious as to how it works.

Thanks guys :D

+7  A: 

Coming from a C++ background the standard definition of NULL when talking about pointers is 0. I would assume that other languages work similarly.

DeusAduro
This is true in most languages, and only really varies across CPUs/architectures.
Al
I *vaguely* remember that some architecture (a PDP variant?) had a null pointer that *wasn't* null, but pointed to a memory address that was equally invalid for dereferencing. Despite Googling I can't find this reference.
Brian Agnew
Don't confuse the value of a null pointer in your C++ code, which must be zero per section 4.10/1 of the C++ standard, and the internal representation of the null pointer that the compiler uses, which can be whatever is convenient.
anon
+7  A: 

In .NET, null values are represented by the "all zero" bit pattern.

This is important, as it means that correctly creating a new array or object only involves wiping the memory to create appropriate default values for all fields before starting to invoke constructors, variable initializers etc where appropriate.

(I've been trying to find where this is specified, and I'm failing so far... but it's the only implementation that makes any sense. I'll keep looking.)

Jon Skeet
In Ecma334, p122 it says: [Note: Initialization to default values is typically done by ... initialize memory to all-bits-zero ... end note]. Note the typical qualifier and the [Note ] context.
Henk Holterman
@Henk: That's C# - I very deliberately restricted myself to .NET instead of C# :)
Jon Skeet
Jon, it seems unlikely that a specific language could implement any of this differently. The note is about 'the memory manager'.
Henk Holterman
@Henk: The point is that a different implementation of C# *could* implement this differently. It could be interpreted and use an "all 1s" bit pattern for example. Different languages on the same CLI implementation would all need to use the same null representation though.
Jon Skeet
+13  A: 

There are two halves to the answer:

  • the value is zero (ie. all bits in the value are zero)
  • zero is never considered a valid address.

The second point is why the answer to your question "But wouldn't that also be a location in memory?" is "No" - it's simply a rule that zero is not considered a valid memory location. Attempting to access it will cause an exception.

Edit: According to Wikipedia (so it must be true 8-) "some architectures use a signed address space and use the most negative value". So it's not necessarily zero on all architectures, but whatever value it has on a given architecture, that value is considered an invalid memory location.

RichieHindle
"it's simply a rule that zero is not considered a valid memory location" <-- this is not true. It's perfectly legitimate (if incredibly disconcerting) to map 0 to a valid page on some systems. However, in the case of C and C++ in particular, the standard demands that 0 be an invalid pointer, so I'm not sure how that interacts.
Promit
The standard doesn't demand that zero is an invalid address, it demands that whenever you compare an address with zero, the compiler has to translate that zero into the standard incorrect address on that architecture. It's a bit coincidence that on x86 Linux the invalid page is also zero, but it doesn't have to be.
kmm
+2  A: 

How does the computer tell that the reference contains null instead of a reference to an object?

According to Wikipedia, the null pointer in certain languages may be a fixed address in memory, which user programs may not access, so if some object points to this address, it's null, otherwise, it isn't.

Dave M
A: 

http://googleit1st.com/search?hl=en&amp;q=null+pointer

Null pointer or null reference Null is a special pointer value (or other kind of object reference) used to signify that a pointer intentionally does not point to (or refer to) an object. Such a pointer is called a null pointer.[1] Many implementations use a value of 0 (all bits zero) to represent the null pointer, as this is at the bottom of the address space of most CPUs (although some architectures use a signed address space and use the most negative value). Many operating systems generate an exception when an attempt is made to access this memory address. Some languages use other nomenclature for such a pointer, e.g., Pascal, Ruby and Lua use nil[2], Visual Basic uses Nothing, and Python uses None. Fortran does not consider null to be a constant, but a property that can be set by the NULLIFY directive and tested by the ASSOCIATED function. Taken from wikipedia

Jason Pyeron
+3  A: 

Not all languages use a particular value, sometimes null is an object. Dynamic languages often have a global object which represents null to which object references are set when they have no value. In these cases method calls can be made on the null object and appropriate responses created.

For example in Ruby there is a singleton called nil and common methods such as or, nil? and to_s all have appropriate default implementations that you would expect if they were called on the null object.

In Java null is specified thoroughly in the virtual machine specification. Its actual value isn't really specified, rather what should happen when a byte code instruction sees it.

Null is normally "handled" by setting an object reference to point to a nil object. But languages with lower abstractions may use zero as the value which is a location in memory, but one which the operating system stops the program from actually writing, instead core dumping or otherwise halting the program.

Paul Keeble
+4  A: 

To answer for c#

The CLR has an opcode for null.

   String s = "ff";

   s = null;

generates this IL

  .locals init ([0] string s)
  IL_0000:  nop
  IL_0001:  ldstr      "ff"
  IL_0006:  stloc.0
  IL_0007:  ldnull
  IL_0008:  stloc.0

as you can see, there is a ldnull opcode which handles the special value of null

sylvanaar
+1  A: 

The first page of RAM is usually protected from user-space programs. That way if a programmer forgets to check for a null pointer, the program will at least cause a page fault, when the program tries to access that location.

So while [0x00000000], is technically a real memory location, most programs can't access it.

x86 processors have a special table there, if I remember correctly.

Brad Gilbert