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?

Community
  • 1
  • 1
Sellorio
  • 1,806
  • 1
  • 16
  • 32

4 Answers4

7

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    And yet, I've **never** heard of any architecture where `NULL` pointer has a value that does not consist of all 0 bits. – mvp May 30 '13 at 06:06
  • 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? – Sellorio May 30 '13 at 06:15
  • @MrUniverse it's a pointer with a value of 0 - not necessarily a null pointer, but probably a null pointer on most platforms. – Luchian Grigore May 30 '13 at 06:18
  • So is it UB or would platforms with non-zero null pointers 'go along with it'. – Sellorio May 30 '13 at 06:20
  • @MrUniverse: It's an unsafely-derived pointer, and dereferencing it yields undefined behavior according to the Standard. Individual platforms may define the behavior. – Ben Voigt May 30 '13 at 06:21
  • @mvp: The original AS/400 didn't use all-zero-bits and maybe still does not. The Pr1me, way back when, did not use all zero bits but the *hardware was changed* to accommodate bad C code, and they added an instruction, `TCNP`, Test C Null Pointer! – torek May 30 '13 at 06:23
  • @BenVoigt Thanks :) Just 1 last lose end, what if I compare this pointer to 0 (literal) after. Would it fail the comparison? – Sellorio May 30 '13 at 06:24
  • 1
    @MrUniverse you simply shouldn't. It could work or not just as well. Don't assign pointers randomly.4 – Luchian Grigore May 30 '13 at 06:26
  • @MrUniverse: I don't understand how a comparison can *fail*. If you compare the pointer to a zero literal, the compiler will generate code that tests whether the pointer is a null pointer (this may *not* be an equality test!). That comparison will succeed, and the result will be either true or false. The program won't crash or generate an exception. Your logic may fail if the comparison result is false, but that is a failure in your logic and not a failure in the comparison. – Ben Voigt May 30 '13 at 06:30
  • @BenVoigt sorry you had to write so much :) - I meant the comparison can return true or false :) (sadly I can no longer edit that comment) – Luchian Grigore May 30 '13 at 06:32
  • @LuchianGrigore Ok got it. That last bit was just some extra curiosity. Thanks for the help :) – Sellorio May 30 '13 at 11:23
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).

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
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

user2384794
  • 53
  • 1
  • 2
  • 13
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

Yogesh Patil
  • 908
  • 4
  • 14