2

When in C++ I declare a null pointer to be int* p=0, does that mean the zero is some special constant of integer pointer type, or does it mean that p is pointing to address 0x0? Of course for that 0x0 would have to be an special address to which C++ never touches during allocation of variables/arrays.

Mat
  • 202,337
  • 40
  • 393
  • 406
smilingbuddha
  • 14,334
  • 33
  • 112
  • 189

8 Answers8

9

The C++ standard defines that the integer constant 0 converts to a null pointer. This does not mean that null pointers point to address 0x0. It just means that the text '0' turns into a null pointer when converted to a pointer.

Of course, making null pointers have a representation other than 0x0 is rather complicated, so most compilers just let 0x0 be the null pointer address and make sure nothing is ever allocated at zero.

Note that using this zero-conversion is considered bad style. Use NULL (which is a preprocessor macro defined as 0, 0L, or some other zero integral constant), or, if your compiler is new enough to support it, nullptr.

bdonlan
  • 224,562
  • 31
  • 268
  • 324
  • 5
    Using zero is not unanimously considered bad style. There are some that consider the macro version is worse. – Paul Manta Sep 25 '11 at 17:21
  • 1
    @Paul Macros aren't pretty but in this case I'd agree that NULL should be used instead of 0. Let's say you switch / upgrade to a compiler that supports `nullptr`. Now which one is easier - search & replace all instances of `0` or `NULL` with `nullptr`? – Praetorian Sep 25 '11 at 17:25
  • @Paul, zero is a bit more ambiguous on reading, I feel. Although NULL does also have the downside that it can hide overloading issues. But I think we can all agree that `nullptr` is best :) – bdonlan Sep 25 '11 at 17:27
  • 3
    @Praetorian I wasn't arguing that NULL is better or worse. I just said that the issue is not settled, so it shouldn't be portrayed as such. Hopefully `nullptr` will make the 'controversy' end. – Paul Manta Sep 25 '11 at 17:28
3

It means that an integral constant expression with value zero has a special meaning in C++; it is called a null pointer constant. when you use such an expression to initialize a pointer with, or to assign to a pointer, the implementation ensures that the pointer contains the appropriately typed null pointer value. This is guaranteed to be a different value to any pointer pointing at a genuine object. It may or may not have a representation that is "zero".

ISO/IEC 14882:2011 4.10 [conv.ptr] / 1:

A null pointer constant is an integral constant expression (5.19) prvalue of integer type that evaluates to zero or a prvalue of type std::nullptr_t. A null pointer constant can be converted to a pointer type; the result is the null pointer value of that type and is distinguishable from every other value of object pointer or function pointer type.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
2

It's a special value, which by the standard is guaranteed to never be equal to a pointer that is pointing to an object or a function. The address-of operator & will never yield the null pointer, nor will any successful dynamic memory allocations. You should not think of it as address 0, but rather as special value that indicates that the pointer is pointing nowhere. There is a macro NULL for this purpose, and the new idiom is nullptr.

jason
  • 236,483
  • 35
  • 423
  • 525
1

It means that it's not pointing to anything.

stiopa
  • 137
  • 9
1

The value of the pointer is just 0. It doesn't necessarily mean it points to address 0x0. The NULL macro, is just a 0 constant.

Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
0

the pointer points to address 0. On most platforms that is very special, but you should use NULL, because it is not always 0 (but very often).

towi
  • 21,587
  • 28
  • 106
  • 187
0

Yes. Zero is a special constant. In fact, it's the only integral constant which can be used, without using explicit cast, in such statements:

int *pi = 0;  //ok
char *pc = 0; //ok
void *pv = 0; //ok
A *pa = 0;    //ok

All would compile fine.

However, if you use this instead:

int *pi = 1;  //error
char *pc = 2; //error
void *pv = 3; //error
A *pa = 4;    //error

All would give compilation error.

In C++11, you should use nullptr, instead of 0, when you mean null pointer.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
0

In your example 'p' is the address of an int. By setting p to 0 you're saying there is an int at address 0. The convention is that 0 is the "not a valid address address", but its just a convention.

In pratice address 0 is generally "unmapped" (that is there is no memory backing that address), so you will get a fault at that address. That's not true in some embedded systems, though.

You could just as well pick any random address (e.g. 0xffff7777 or any other value) as the "null" address, but you would be bucking convention and confusing a lot of folks that read your code. Zero is generally used because most languages have support for testing is-zero is-not-zero efficiently.

See this related question: Why is address zero used for the null pointer?

Community
  • 1
  • 1
P.T.
  • 24,557
  • 7
  • 64
  • 95