0

Is a pointer, which points to 0 always invalid? The addresses are used for very specific things, right? So if a developer tried to define a pointer to a variable in his scope, it should always be invalid, right?

int main() {
    int *ptr = (int *)0;
}

Is the address 0x0000 protected?

The reason I am asking is, because I have a struct with a union, and the values of the union can either be a double equal to 0, or a pointer to another struct.

Clebo Sevic
  • 581
  • 1
  • 7
  • 17
  • 4
    Are they __always__ invalid? Certainly not, microcontrollers, for example, might have something mapped to address 0. – tkausl Sep 29 '21 at 15:59
  • 3
    I don't see why would it matter in your case. If the value of the `double` in union is `0`, then it can be a valid `double` but an invalid pointer. – Eugene Sh. Sep 29 '21 at 16:01
  • A double with value zero won't be represented as 0x0000000000000000. It'll be better to add an indicator that tells you how to interpret the memory location – Ronald Sep 29 '21 at 16:01
  • 1
    This is sort of a tricky question - a complete answer would have to bring in null pointer constants (and how they are possibly different from address 0), the representation of `double`, addresses, and undefined behavior. But the short answer is - don't dereference that pointer. In fact, if you most recently wrote to the `double` member of the union, don't even *access* the pointer member. – Nate Eldredge Sep 29 '21 at 16:26
  • Pointer to 0 is not invalid : it is used in chained lists to check for end. The problem occurs generally when you want to access data at 0 : exc__.bad_access or other exception. – Ptit Xav Sep 29 '21 at 17:50
  • 1
    See [**Is NULL always zero in C?**](https://stackoverflow.com/questions/9894013/is-null-always-zero-in-c) – Andrew Henle Sep 29 '21 at 19:22
  • 1
    `(int *)0` is a special case. This makes a *null pointer* , it does not make a pointer to address zero. – M.M Sep 29 '21 at 19:39
  • 1
    @Ronald in IEEE754 , all-bits-zero is a double with value zero. (But there can be other representations of value zero; and there can be non-IEEE754 systems) – M.M Sep 29 '21 at 19:43
  • @M.M Thank you for pointing this out. I knew about the other representation(s) of zero, but didn't know about the low values. The rest of my comment remains valid though. – Ronald Sep 30 '21 at 10:06

2 Answers2

3

Simplifying:

This pointer is called NULL pointer and it is valid. Invalid is to dereference it.

Bear in mind that not every operation which looks like dereferencing is actually dereferencing and is invalid.

int *p = NULL;

size_t psize = sizeof(*p);  // valid, it is not dereferencing.

int x = *p; // invalid

Some very low-level programming requires reading or writing to the address 0 converted to the pointer.

uint32_t initial_stack_pointer = *(volatile uint32_t *)0UL;

It reads the initial (boot) value of the stack register from the Cortex-M vector table.

0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1
    *This pointer is called NULL pointer* No, it's not. `0` is a ["null pointer constant"](https://port70.net/~nsz/c/c11/n1570.html#6.3.2.3p3), but that's [not the same thing as `NULL`](https://port70.net/~nsz/c/c11/n1570.html#7.19p3). Note well that [`NULL` is not defined to be zero.](https://stackoverflow.com/questions/9894013/is-null-always-zero-in-c). – Andrew Henle Sep 29 '21 at 19:20
  • 2
    @AndrewHenle it is simplification for OPs language knowledge level. – 0___________ Sep 29 '21 at 19:26
  • Fair point. Edit your question to I can change my vote. ;-) – Andrew Henle Sep 29 '21 at 19:49
  • `sizeof *p` is dereferencing, but this is a context where dereferencing null is permitted (because the expression is not evaluated) – M.M Oct 01 '21 at 00:09
1

In practice, in 2021, an address with all zero bits is invalid and practically is the NULL pointer (on laptops, desktops, supercomputers).

In theory, this is false (e.g. C compilers in the previous century for the Intel 286 processors had pointers of different width than int or long). Read for example the n1570 C standard.

The reason I am asking is, because I have a struct with a union, and the values of the union can either be a double equal to 0, or a pointer to another struct.

This will work on all the computers I have access to (in 2021).

But you could try static source code analyzers like Frama-C (or perhaps look into Bismon, or the DECODER project).

For C on microcontrollers like AVR or 8051, things are different. The all zero bits address could be dereferenced

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547