2

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?

4
  • What kind of answer are you looking for? A list? Commented May 30, 2013 at 6:01
  • 1
    You can read: c-faq.com/null/machnon0.html Commented May 30, 2013 at 6:01
  • you are correct in saying NULL is actually implementation dependant. check <cstddef> for c++ and <stddef.h> for C. but in c/c++ model you can think of NULL being 0. Commented May 30, 2013 at 6:01
  • @Niko Not a full list but some reference material. Commented May 30, 2013 at 6:03

4 Answers 4

8

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.

Sign up to request clarification or add additional context in comments.

11 Comments

And yet, I've never heard of any architecture where NULL pointer has a value that does not consist of all 0 bits.
What if you type cast and integer of value 0 to a pointer and assign that? At that point it is out of the compiler's hands, what would happen?
@MrUniverse it's a pointer with a value of 0 - not necessarily a null pointer, but probably a null pointer on most platforms.
So is it UB or would platforms with non-zero null pointers 'go along with it'.
@MrUniverse you simply shouldn't. It could work or not just as well. Don't assign pointers randomly.4
|
4

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).

Comments

0

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

1 Comment

Physically they are the garbage values that are not to use.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.