简体   繁体   中英

Is it dangerous to use NULL pointer dereferencing at rvalue side ?

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 ?

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.

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.

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

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?

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.

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, eg

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM