1

I have the following code.

int *a = NULL;
int b;

b = *a;

Is it undefined in any way? I am trying to access the location pointed by a only for reading. What wrong it can do ?

bubble
  • 3,408
  • 5
  • 29
  • 51

6 Answers6

5

Is it undefined in any way?

Yes, the behavior is undefined.

I am trying to access the location pointed by a only for reading.

The location pointed to does not exist. There's nothing there. To read something, there has to be a value to read, but a null pointer points... nowhere. It doesn't point at anything.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Cant we say that the staring address or the very first byte of our system is something like 0x00000000 ? – bubble Aug 28 '12 at 17:38
  • @bubble, no for the distinction of null pointer and the address with value `0` see my answer. (James no offence intended, +1 anyhow) – Jens Gustedt Aug 28 '12 at 20:11
  • if `NULL` is defined somethig like `#define NULL 0`, Still will you say same thing – bubble Aug 29 '12 at 03:24
  • Yes. The literal `0`, when used as a pointer, is a null pointer constant. It doesn't matter what the actual value of a null pointer is at runtime. In code `0` when used as a pointer always means "null pointer." – James McNellis Aug 29 '12 at 21:29
  • @JamesMcNellis you mean then we can never refer to a location having address 0x00000000 – bubble Aug 30 '12 at 10:19
3

Implementation chooses the physical values of null-pointers specifically to make them "point" to address(es) that contains nothing of any value on the given platform. That means that there's nothing to see there. Moreover, on a modern virtual memory platform there might not even be a specific "there" associated with that address.

If you are doing it out of pure curiosity, it still won't work on a typical modern platform: that [virtual] memory region is typically "not yours", meaning that hardware/OS protection mechanism will prevent you from reading it (by crashing your program).

Meanwhile, the language states that any attempts to dereference a null-pointer leads to undefined behavior, regardless of what you are dereferencing it for.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • On older operating systems (DOS/Win16), the OS had no way to enforce this. Writing into a NULL pointer could create all kinds of havoc. This remains true in some embedded systems (lacking an MMU) today. – sfstewman Aug 28 '12 at 16:59
2

Yes, it's totally undefined behaviour. You are dereferencing a null pointer. Anything could happen.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

Don't dereference NULL pointers. It's like crossing the streams, but worse. I'm not even sure what you're trying to accomplish? Are you trying to initialize two pointers to NULL?

Joe K
  • 104
  • 5
  • Reading from a pointer that is pointed at NULL is dangerous because you don't know what you're actually going to get, what the size of it is, maybe its an executable. Generally you'll get a GPF error as the pointer will be outside of your memspace just on a read, and if you tried to write to that location, you very well run the risk of accidentally the whole thing. – Joe K Aug 28 '12 at 19:05
  • +1 for comparing a NULL pointer dereference to "crossing the streams" – 5StringRyan Aug 28 '12 at 19:58
0

The result is undefined and will be different under different platforms.

NULL is equal to 0. Therefore saying int* a = NULL means that you make a pointer to address 0, which should contain an integer.

By dereferecing a by b = *a you instruct the program to go to the address 0 and get the data.

Now, depending on the platform, address 0 will most probably be inaccessible to the user code. If you are not running on top of an operating system (as the case is for example on a microcontroller environment), then address 0 is normally the beginning of the internal ROM. In other cases it can be the beginning of the stack etc.

In most of the cases you are going to get a null pointer exception when you dereference a null pointer.

Lefteris
  • 873
  • 2
  • 8
  • 29
  • No @Lefteris, it is not necessarily the `0` address, depends on the internal representation of null pointer constants on the platform. – Jens Gustedt Aug 28 '12 at 20:10
0

You have two alarms that pop up here. NULL and all other forms of null pointers are strickly forbidden by the standard. Not because they are often realized internally by a bit pattern of all 0 for the representation of such a pointer but because that is the one-and-single reserved value for pointers that indicate that it isn't pointing anywhere. Don't do this.

Having a pointer point to the address that corresponds to the integer value 0 is a different thing. You could only enforce such a value and interpretation of a point by some trick, e.g

union over {
  int *a;
  uintptr_t b;
} ov;

ov.b = 0;
int c = *ov.a;

What then happens is completely depending on your platform. If 0 is a valid address in your address space this could work, it probably wouldn't. Don't do it unless you know better.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177