py2akv wrote: > Dear Tujare, > > 1. I'm no pointer specialist, but I suspect, first and foremost, null pointer > is a misnomer, because a pointer always points to something. >
A pointer points to a location in memory. That location may or may not be valid, and there may or may not be an object at that location. Specifically, a pointer to memory location zero has special meaning: this is defined to be a null pointer. It is not valid in the sense of reaching an object or primitive, but it is valid in that it has special meaning both to the language and to the standard libraries. > Let's remember a pointer is, per se, also an object, only carrying the > address of another object. > A pointer is not an object, it is a primitive. Specifically, it is an integral type that has special meaning to the compiler: it holds an address. This is why the compiler treats it differently, despite just being an integer of some length. > Pointing to an address is one thing, but pointing to an object is another > thing. > Objects live at addresses. Pointers point to addresses. What lives at a specific address may or may not be an object. > It's up to the compiler, I think, to choose where a null pointer should point > to. > No, it is up to the standard. A null pointer MUST point to memory address zero. From the C standard ISO/IEC 9899:1999(E), 6.3.2.3: "An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant." From the C++ standard ISO/IEC 14882:2003(E) 4.10: "A null pointer constant is an integral constant expression (5.19) rvalue of integer type that evaluates to zero." > A null pointer is something pointing to no object? > A pointers points to an address, not an object. Unless there is a serious problem, there generally is an object (or primitive) at that address unless it is a null pointer. A null pointer is special because nothing lives at memory address zero: if my operating system knowledge is correct, all modern OSes map the first page of memory to a special area that is all zeros, and protects it: you cannot derefence a null pointer, not even to a primitive integer. > Mind, malloc() and family points to an unspecified, but known, block of > memory and that's why it gives you a void pointer. > > It's your turn to decide what to make with it, if you need to, casting it to > an appropriate type, perhaps, all according to your convenience. > Correct. This is one reason why one should use new and delete instead of malloc() and free(): however, the OP was unclear as to which language the question pertained. -- John Gaughan http://www.jtgprogramming.org/
