Sellorio
Sellorio

Reputation: 1950

C++ What are the NULL pointer values?

I recently saw an article that mentioned that null pointers in C/C++ were actually not simply zero but were interpreted by the compiler to whichever allocation address was null for the platform.

https://stackoverflow.com/a/2760172/2027262

This tied in with something I saw when debugging c++ in Visual Studio where a pointer's value was 0xCACACACA (or something like that) when it was a bad pointer (but this could just be a value displayed for our benefit).

So long-story-short, what are the REAL null pointer addresses for a platform (such as windows)? Or was I misunderstanding the answer completely?

EDIT:

Also, (just as an extension) what did this?

int i = 0;

// pretend to do some stuff with i

char* = (char*)i;

Would the compiler set the pointer to null at run time or would it set it to zero? And if it does the latter is that UB? Will it break the program?

Upvotes: 2

Views: 1678

Answers (4)

Ben Voigt
Ben Voigt

Reputation: 283634

This question previously appeared on the comp.lang.c newsgroup. You can read the archive with Google Groups

In that thread, Paul Sand quoted another source, "Portable C" by H. Rabinowitz and Chaim Schaap, as follows:

Certain Prime computers use a value different from all-bits-0 to encode the null pointer. Also, some large Honeywell-Bull machines use the bit pattern 06000 to encode the null pointer. On such machines, the assignment of 0 to a pointer yields the special bit pattern that designates the null pointer.

Similarly, (char *)0 yields the special bit pattern that designates a null pointer.

Where you'd commonly see non-null pointers is when working with physical memory addresses (that is, when there is no MMU or in kernel mode bypassing the MMU) and the machine has memory-mapped I/O at or near address 0. You want a null pointer to be way out in no-man's land, so that if you offset it (e.g. structure member access via a pointer) you won't get any useful address.

For your specific question, only an integral constant expression with value 0 is interpreted as a null pointer. So

char* p = (char*)i;

does not portably make p a null pointer (i.e. the Standard makes no such guarantee, but your particular compiler may).

Upvotes: 4

Yogesh Patil
Yogesh Patil

Reputation: 888

The original meaning of NULL character was like NOP—when sent to a printer or a terminal, it does nothing (some terminals, however, incorrectly display it as space)

Well in C, C++; A null-pointer constant is either an integral constant expression that evaluates to zero (such as 0 or 0L), or a value of type nullptr_t (such as nullptr).

A null pointer constant can be converted to any pointer type (or pointer-to-member type), which acquires a null pointer value. This is a special value that indicates that the pointer is not pointing to any object.

Refer this http://bytes.com/topic/c/answers/213647-null-c

Upvotes: 0

Luchian Grigore
Luchian Grigore

Reputation: 258608

The 0xCACACACA generated by Visual Studio is usually there for un-initialized pointers, for precisely this reason. When you see this, you know something is wrong. If it initialized it to 0, it can very well be expected behavior. This is only done in debug node, with full optimization the value of an uninitialized pointer would be just garbage.

And yes, NULL pointers don't have a value of 0 per say. 0 is just a literal. The actual value can be different. But the compiler is smart enough to abstract that away, so even if you have a NULL pointer whose value isn't really 0 (as in the number 0), comparing it to 0 or NULL would still yield true. It's better to think in terms of nullptr really.

Upvotes: 8

user2384794
user2384794

Reputation: 63

A null pointer has a value reserved for indicating that the pointer does not refer to a valid object. Null pointers are routinely used to represent conditions such as the end of a list of unknown length or the failure to perform some action; this use of null pointers can be compared to null able types and to the Nothing value in an option type.

i hope this will clear you but for more please visit this

Upvotes: 0

Related Questions